从片段错误开始活动

时间:2014-12-22 18:39:18

标签: android error-handling android-fragmentactivity

我想从片段开始一个活动......这是我的代码

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ShareCompat.IntentBuilder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.PopupMenu.OnMenuItemClickListener;

public class HomeFragment extends Fragment {

    public HomeFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container,
                false);
        return rootView;
    }

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

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.home_add_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.acc_id:
            Intent intent = new Intent(getActivity(), AccountActivity.class);
            startActivity(intent);
            break;

        case R.id.inc_id:
            break;
        }
        return true;
    }

}

但是当我按下片段中的菜单项时,它会崩溃。我在android清单中添加了活动。

12-22 20:24:33.975: I/Process(979): Sending signal. PID: 979 SIG: 9 12-22 20:26:58.665: I/Choreographer(1002): Skipped 91 frames! The application may be doing too much work on its main thread. 12-22 20:26:58.775: D/gralloc_goldfish(1002): Emulator without GPU emulation detected. 12-22 20:26:58.888: D/dalvikvm(1002): GC_CONCURRENT freed 127K, 3% free 8309K/8519K, paused 42ms+19ms, total 302ms 12-22 20:26:59.105: I/Choreographer(1002): Skipped 35 frames! The application may be doing too much work on its main thread. 12-22 20:27:01.824: I/Choreographer(1002): Skipped 83 frames! The application may be doing too much work on its main thread. 12-22 20:27:03.054: I/Choreographer(1002): Skipped 31 frames! The application may be doing too much work on its main thread. 12-22 20:29:02.275: I/Choreographer(1050): Skipped 90 frames! The application may be doing too much work on its main thread. 12-22 20:29:02.405: D/gralloc_goldfish(1050): Emulator without GPU emulation detected. 12-22 20:29:02.485: D/dalvikvm(1050): GC_CONCURRENT freed 130K, 3% free 8309K/8519K, paused 42ms+33ms, total 287ms 12-22 20:29:02.815: I/Choreographer(1050): Skipped 36 frames! The application may be doing too much work on its main thread. 12-22 20:29:04.924: I/Choreographer(1050): Skipped 90 frames! The application may be doing too much work on its main thread. 12-22 20:29:06.094: I/Choreographer(1050): Skipped 41 frames! The application may be doing too much work on its main thread. 12-22 20:29:06.364: D/AndroidRuntime(1050): Shutting down VM 12-22 20:29:06.374: W/dalvikvm(1050): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 12-22 20:29:06.524: E/AndroidRuntime(1050): FATAL EXCEPTION: main 12-22 20:29:06.524: E/AndroidRuntime(1050): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.GeorgeZoiade.tubapps/com.GeorgeZoiade.tubapps.AccountActivity}: java.lang.ClassCastException: com.GeorgeZoiade.tubapps.AccountActivity cannot be cast to android.app.Activity 12-22 20:29:06.524: E/AndroidRuntime(1050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983) 12-22 20:29:06.524: E/AndroidRuntime(1050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 12-22 20:29:06.524: E/AndroidRuntime(1050): at android.app.ActivityThread.access$600(ActivityThread.java:130) 12-22 20:29:06.524: E/AndroidRuntime(1050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 12-22 20:29:06.524: E/AndroidRuntime(1050): at android.os.Handler.dispatchMessage(Handler.java:99) 12-22 20:29:06.524: E/AndroidRuntime(1050): at android.os.Looper.loop(Looper.java:137) 12-22 20:29:06.524: E/AndroidRuntime(1050): at android.app.ActivityThread.main(ActivityThread.java:4745) 12-22 20:29:06.524: E/AndroidRuntime(1050): at java.lang.reflect.Method.invokeNative(Native Method) 12-22 20:29:06.524: E/AndroidRuntime(1050): at java.lang.reflect.Method.invoke(Method.java:511) 12-22 20:29:06.524: E/AndroidRuntime(1050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 12-22 20:29:06.524: E/AndroidRuntime(1050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 12-22 20:29:06.524: E/AndroidRuntime(1050): at dalvik.system.NativeStart.main(Native Method) 12-22 20:29:06.524: E/AndroidRuntime(1050): Caused by: java.lang.ClassCastException: com.GeorgeZoiade.tubapps.AccountActivity cannot be cast to android.app.Activity 12-22 20:29:06.524: E/AndroidRuntime(1050): at android.app.Instrumentation.newActivity(Instrumentation.java:1053) 12-22 20:29:06.524: E/AndroidRuntime(1050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974) 12-22 20:29:06.524: E/AndroidRuntime(1050): ... 11 more 12-22 20:29:08.804: I/Process(1050): Sending signal. PID: 1050 SIG: 9

1 个答案:

答案 0 :(得分:2)

看起来AccountActivity并未延伸android.app.Activity