当前位置: 首页 > 知识库问答 >
问题:

我如何在其他类中做一个碎片事务?

聂风史
2023-03-14

我想创建一个类,它的功能只是做一个碎片事务,但我有一个错误。我的课是下一个:

package net.elinformaticoenganchado.sergio.crossfighters.common;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import net.elinformaticoenganchado.sergio.crossfighters.R;
import net.elinformaticoenganchado.sergio.crossfighters.statics.Main;

/**
 * @author Sergio Herrero Cruz <sergioherre96@gmail.com>
 * @package net.elinformaticoenganchado.sergio.crossfighters.common
 */
public class FragmentsGoTo extends FragmentActivity{


    public void goToMainFragment() {
        Fragment fragment = new Main();
        FragmentTransaction ftConfig = getSupportFragmentManager().beginTransaction();
        ftConfig.replace(R.id.main_frame, fragment);
        ftConfig.commit();
    }
}
package net.elinformaticoenganchado.sergio.crossfighters.configuration;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import net.elinformaticoenganchado.sergio.crossfighters.R;
import net.elinformaticoenganchado.sergio.crossfighters.common.FragmentsGoTo;
import net.elinformaticoenganchado.sergio.crossfighters.entity.Configuration;

/**
 * @author Sergio Herrero Cruz <sergioherre96@gmail.com>
 */
public class ConfigurationEdit extends Fragment implements View.OnClickListener{

    EditText name;

    /**
     * @param inflater           The LayoutInflater object that can be used to inflate
     *                           any views in the fragment,
     * @param container          If non-null, this is the parent view that the fragment's
     *                           UI should be attached to.  The fragment should not add the view itself,
     *                           but this can be used to generate the LayoutParams of the view.
     * @param savedInstanceState If non-null, this fragment is being re-constructed
     *                           from a previous saved state as given here.
     * @return Return the View for the fragment's UI, or null.
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.configuration_edit, container, false);

        name = (EditText) view.findViewById(R.id.configuration_edit_name);
        Button save = (Button) view.findViewById(R.id.configuration_edit_button);

        save.setOnClickListener(this);

        return view;
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.configuration_edit_button) {
            Configuration config = new Configuration(name.getText().toString());
            config.save();
            FragmentsGoTo fragmentsGoTo = new FragmentsGoTo();
            fragmentsGoTo.goToMainFragment();
        }
    }

}

进程:net.elinformaticoenganchado.sergio.crossfights,PID:5116 java.lang.runtimeException:无法启动活动ComponentInfo{net.elinformaticoenganchado.sergio.crossfights/net.elinformaticoenganchado.sergio.crossfights.mainactivity}:java.lang.nullpointerException:试图在Android.app.activitythread.performLaunchActivity(activitythread.2416)的.handlelaunchactivity(activityThread.java:2476),android.app.activityThread.-Wrap11(activityThread.java),android.app.activityThread.$h.handleMessage(activityThread.java:1344),android.os.handler.dispatchMessage(handler.java:102),android.os.handler.loper.loop(looper.java:148),android.app.activityThread.main(ler.run(zygoteinit.java:726)在com.android.internal.os.zygoteinit.main(zygoteinit.java:616)由:java.lang.nullpointerexception引起:试图在net.elinformaticoenganchado.sergio.crossfigters.mainactivity.oncreate(mainActivity.java:42),在Android.app.activity.performCreate(activity.java:6237),在Android.app.instrumentation.callActivityonCreate(instrumentation.java:1107),在Android.app.activityThread.performLaunchActivity(activityThread.java:2369),在Android.app.activityThread.handleLaunchActivity(activityThread.java:2476),在 在Android.app.activitythread.-Wrap11(activitythread.java) 在Android.app.activitythread$h.handleMessage(activitythread.java:1344) 在Android.os.handler.DispatchMessage(Handler.java:102) 在Android.os.looper.loop(looper.java:148) 在Android.app.activitythread.main(activitythread.java:5417) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) 在com.android.internal.os.zygoteinit.main(zygoteinit.java:616)

所以我怎样才能在java中没有错误。

谢谢你。

编辑:添加mainActivity类:

package net.elinformaticoenganchado.sergio.crossfighters;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import net.elinformaticoenganchado.sergio.crossfighters.common.FragmentsGoTo;
import net.elinformaticoenganchado.sergio.crossfighters.configuration.ConfigurationEdit;
import net.elinformaticoenganchado.sergio.crossfighters.entity.Configuration;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        FragmentsGoTo fragmentsGoTo = new FragmentsGoTo();
        //@Todo change for start
        //Configuration.deleteAll(Configuration.class);

        if (Configuration.count(Configuration.class) == 0) {
            Fragment fragment = new ConfigurationEdit();
            FragmentTransaction ftConfig = getSupportFragmentManager().beginTransaction();
            ftConfig.replace(R.id.main_frame, fragment);
            ftConfig.commit();
        } else {
            fragmentsGoTo.goToMainFragment();
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

共有1个答案

司马辉
2023-03-14

因为我的评论帮助修复了:

这不是实例化活动的方式(调用new)。谷歌一点,使用意图。您需要一个包含片段的活动,然后交换其中的片段。

 类似资料:
  • 当我将一个片段(它是带有背景的全屏)显示在另一个片段(我们称之为main)上时,我的main片段仍然会对单击做出反应(即使我们没有看到按钮,我们也可以单击它)。 问题:如何防止点击第一(主)片段?

  • 我想知道在ReactJSJSX中是否可以嵌套if else if? 我尝试过各种不同的方法,但我无法让它发挥作用。 我正在寻找 我已经试过了,但是我不能把它渲染出来。我试过各种方法。添加嵌套if后,它总是会中断。 使现代化 最后,我选择了将其移动到renderContent并调用该函数的解决方案。不过,这两个答案都有效。我想我可以使用内联解决方案,如果它用于简单的渲染和更复杂的情况下的渲染内容。

  • 我正在使用导航组件,我有片段A和片段B,从片段A我发送一个对象到带有安全参数的片段B,并导航到它。 现在,在我的片段B中进行了一些逻辑之后,我想再次将这些数据传递给我使用的片段A 然后在片段A中,我用 现在,这很好,但是如果我从片段A转到片段B并按下后退按钮,上面的观察者会再次激发,复制我的当前数据,当我只从片段B按下btn\u add\u to\u cart按钮时,有没有办法只激发这个观察者?

  • 我有一个很大的问题,我不知道我是否错过了一些明显的东西或什么,但我不能发现我的错误。我有类SPN、A和B。我重载了2次operator=。我想将A类型或B类型作为参数传递。 它不会抛出任何错误。但是如果我在类B或A中尝试make operator(),则使用参数SPN,如下所示: 它会抛出SPN未命名类型的错误。我甚至不能在A类或B类中创建类SPN对象。也许它不是如何客观编程工作,所以我想得到它,

  • 我读过很多关于这方面的文章,但也有2012年或更早的文章。 (我只是打算从数据库中读取和插入一些数据。)

  • 本文向大家介绍如何做图片预览,如何放大一个图片?相关面试题,主要包含被问及如何做图片预览,如何放大一个图片?时的应答技巧和注意事项,需要的朋友参考一下 https://blog.csdn.net/Bule_daze/article/details/79543690