在活动中通过onclicklistener切换片段

时间:2014-01-27 22:22:15

标签: android button android-activity fragment switching

我有片段的应用程序,我想通过按钮操作的活动在它们之间切换。这是我的尝试。我该怎么做?

public class StartActivity extends Activity {

    public static Context appContext;

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

        //Fragment
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction =
            fragmentManager.beginTransaction();

        MeinFragment A_fragment = new MeinFragment();
        fragmentTransaction.replace(android.R.id.content, A_fragment);

        fragmentTransaction.commit();

        ImageButton BTN1 = (ImageButton) findViewById(R.id.leftButton);
        BTN1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                SearchFragment A_fragment = new SearchFragment();
                fragmentTransaction.replace(android.R.id.content, A_fragment);

            }
        }); 
    }
}

我的第二次尝试,但应用程序崩溃了。

公共类StartActivity扩展Activity {     public static Context appContext;

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

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();


    MeinFragment A_fragment = new MeinFragment();


    fragmentTransaction.replace(android.R.id.content, A_fragment);

    fragmentTransaction.commit();


  //Fragments changing
    ImageButton BTN1 = (ImageButton) findViewById(R.id.leftButton);
   BTN1.setOnClickListener(new OnClickListener() {
       public void onClick(View v) {



          FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction =                        fragmentManager.beginTransaction();


          SearchFragment B_fragment = new SearchFragment();
            fragmentTransaction.replace(android.R.id.content, B_fragment);
            fragmentTransaction.commit();

       }
       });

}

}

和logcat:

01-27 17:37:51.412:E / AndroidRuntime(3339):致命异乎寻常:主要 01-27 17:37:51.412:E / AndroidRuntime(3339):java.lang.RuntimeException:无法启动活动ComponentInfo {de.arvidg.exampleactionbartabs / de.arvidg.exampleactionbartabs.StartActivity}:java.lang.NullPointerException 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256) 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.os.Handler.dispatchMessage(Handler.java:99) 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.os.Looper.loop(Looper.java:137) 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.app.ActivityThread.main(ActivityThread.java:5103) 01-27 17:37:51.412:E / AndroidRuntime(3339):at java.lang.reflect.Method.invokeNative(Native Method) 01-27 17:37:51.412:E / AndroidRuntime(3339):at java.lang.reflect.Method.invoke(Method.java:525) 01-27 17:37:51.412:E / AndroidRuntime(3339):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 01-27 17:37:51.412:E / AndroidRuntime(3339):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-27 17:37:51.412:E / AndroidRuntime(3339):at dalvik.system.NativeStart.main(Native Method) 01-27 17:37:51.412:E / AndroidRuntime(3339):引起:java.lang.NullPointerException 01-27 17:37:51.412:E / AndroidRuntime(3339):at de.arvidg.exampleactionbartabs.StartActivity.onCreate(StartActivity.java:52) 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.app.Activity.performCreate(Activity.java:5133) 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 01-27 17:37:51.412:E / AndroidRuntime(3339):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 01-27 17:37:51.412:E / AndroidRuntime(3339):... 11 more

2 个答案:

答案 0 :(得分:0)

fragmentTransaction
    .beginTransaction()
    .replace(android.R.id.content, A_fragment)
    .commit();

您需要开始事务并提交以完成操作。

答案 1 :(得分:0)

请参阅javadoc for FragmentTransaction.beginTransaction():

注意:片段事务只能在活动保存其状态之前创建/提交。如果您尝试在Activity.onSaveInstanceState()之后提交事务(并且在以下Activity.onStart或Activity.onResume()之前,则会出现错误。这是因为框架负责将当前片段保存在状态中,如果在保存状态后进行了更改,则它们将丢失。