2

This is my code for an android application:

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();
}

Where the file browser only has this:

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);       
}

The thing is if I comment the ArrayAdapter constructor the program does not crash If I uncomment it, it crashes with the following error:

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

Does any one have any Idea what I could be doing wrong?

Thanks for any help!

UPDATE:

The problem seems to be specifically that getActivity returns null? Any Idea why this is?

aarelovich
  • 5,140
  • 11
  • 55
  • 106
  • 3
    I've solved my problem It seems the Fragment was not attached to the activity yet and that is why I was getting a null pointer. I fixed it by simply copying and pasting my test() code onto the onAttach(Activity activiy) method and it worked just fine! It seems I didn't do my research well enough as the answer I found it here: http://stackoverflow.com/questions/6215239/getactivity-returns-null-in-fragment-function Thanks for any help! – aarelovich Jun 11 '11 at 13:39
  • The solution for the same problem was posted in another thread: http://stackoverflow.com/a/19705346/684582 – Alécio Carvalho Oct 31 '13 at 11:48

0 Answers0