尝试打开片段时应用程序崩溃

时间:2015-03-26 14:42:15

标签: android android-intent android-fragments android-activity

我正在尝试使用

打开活动中的片段
   buttonreturn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Chapter1Activity.this, Chapter1.class);
            startActivity(intent);

第1章是我试图访问的片段 但每当我按下使其打开的按钮时,应用程序崩溃。 任何想法?

修改

logcat的:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.boszcorp.revappbiology/com.boszcorp.revappbiology.FooFragment}: java.lang.ClassCastException: com.boszcorp.revappbiology.FooFragment cannot be cast to android.app.Activity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
        at android.app.ActivityThread.access$900(ActivityThread.java:147)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5256)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
 Caused by: java.lang.ClassCastException: com.boszcorp.revappbiology.FooFragment cannot be cast to android.app.Activity
        at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$900(ActivityThread.java:147)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5256)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

1 个答案:

答案 0 :(得分:1)

因为片段的使用是错误的。它应该是这样的

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment newFragment = MyFragment.newInstance("From Arguments");
    ft.add(R.id.created, newFragment);
    ft.commit();

http://developer.android.com/reference/android/app/Fragment.html