FragmentList的ArrayAdapter崩溃了应用程序

时间:2011-06-11 13:12:38

标签: android

这是我的Android应用程序代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Attempting to set the Fragment
    System.err.println("Starting");
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    System.err.println("Adding the fragment");
    FileBrowser fb = new FileBrowser();
    ft.add(R.id.flFBrowser,fb);
    ft.commit();
    System.err.println("Done");

    fb.test();
}

文件浏览器只有这个:

public void test(){
    System.err.println("Entre a test!!!");
    String[] MyList = {"Hello","world","of","the","lists"};
    System.err.println("File Row ID" + Integer.toString(R.layout.file_row));
    ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(), R.layout.file_row, MyList);
    //setListAdapter(aa);       
}

问题是,如果我评论ArrayAdapter构造函数,程序不会崩溃如果我取消注释它,它会崩溃并出现以下错误:

06-11 10:12:38.150: ERROR/AndroidRuntime(4804): FATAL EXCEPTION: main
06-11 10:12:38.150: ERROR/AndroidRuntime(4804): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ccr/com.ccr.Main}: java.lang.NullPointerException
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.os.Looper.loop(Looper.java:132)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.app.ActivityThread.main(ActivityThread.java:4025)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at java.lang.reflect.Method.invoke(Method.java:491)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at dalvik.system.NativeStart.main(Native Method)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804): Caused by: java.lang.NullPointerException
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:314)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at com.ccr.FileBrowser.test(FileBrowser.java:13)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at com.ccr.Main.onCreate(Main.java:24)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
06-11 10:12:38.150: ERROR/AndroidRuntime(4804):     ... 11 more

有没有人知道我可能做错了什么?

感谢您的帮助!

更新:

问题似乎是getActivity返回null? 任何想法为什么会这样?

0 个答案:

没有答案