1

I was developing an Android app and while modifying a Fragment's code, I am now getting hard crashes whenever I try to launch the app. The strange this is, the NullPointerException is being thrown before it ever actually reaches my code, as when I go into debug mode the Exception is thrown before I reach OnCreate() for my MainActivity. Particularly baffling is I wasn't messing with anything in either the MainActivity nor its layout.

Stack trace below. Any ideas?

07-30 15:39:52.753: E/AndroidRuntime(5348): FATAL EXCEPTION: main
07-30 15:39:52.753: E/AndroidRuntime(5348): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.legacy/com.example.legacy.MainActivity}: java.lang.NullPointerException
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.os.Looper.loop(Looper.java:137)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.app.ActivityThread.main(ActivityThread.java:4950)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at java.lang.reflect.Method.invoke(Method.java:511)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at dalvik.system.NativeStart.main(Native Method)
07-30 15:39:52.753: E/AndroidRuntime(5348): Caused by: java.lang.NullPointerException
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:656)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.support.v4.app.Fragment.performStart(Fragment.java:1481)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1866)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:568)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.app.Activity.performStart(Activity.java:5189)
07-30 15:39:52.753: E/AndroidRuntime(5348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)

Thanks in advance.

JuniorIncanter
  • 1,569
  • 2
  • 16
  • 27

3 Answers3

0

IF this is pre Honeycomb then YOU MUST use the support library (I think you are doing this already).

IF you are using the support library fragments then YOU MUST use a FragmentActivity and not an Activity from which to launch your fragments.

IF you are using setRetainInstance(true) then please would you remove it and try again.

(This is the link I suggested that actually helped resolve the problem):

Android FragmentManager BackStackRecord.run throwing NullPointerException

Community
  • 1
  • 1
IanB
  • 3,489
  • 1
  • 20
  • 24
  • I'm already using the support library, and my MainActivity already extends FragmentActivity. – JuniorIncanter Jul 31 '13 at 00:02
  • 1
    Good. Please look at this which MAY be relevant: http://stackoverflow.com/questions/13393693/android-fragmentmanager-backstackrecord-run-throwing-nullpointerexception – IanB Jul 31 '13 at 00:36
  • Oh - are you using setRetainInstance(true) ? – IanB Jul 31 '13 at 00:41
  • I'm not calling setRetainInstance(true) manually anywhere, so unless something else is calling it I'm not. – JuniorIncanter Jul 31 '13 at 00:59
  • Sheldon, that was it. Apparently any call off of FragmentTransaction (like FragmentTransaction.remove(null)) crashes the application and prevents you from even debugging. Nasty bug, I'm surprised they allow that. – JuniorIncanter Jul 31 '13 at 01:41
0

Had this exact same issue, and none of the suggestions above helped me. What did help me was examining the imports for my fragments:

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;

public class SelectionFragment extends Fragment{

As can be seen, the import for Fragment is android.app.Fragment when it should be android.support.v4.app.Fragment.
So changing the code as follows resolved my issue:

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;

public class SelectionFragment extends Fragment{

The first import is probably added automatically by Eclipse.

Community
  • 1
  • 1
vvbYWf0ugJOGNA3ACVxp
  • 1,086
  • 10
  • 23
  • What fixed my problem was actually posted by another user as a comment (and apparently they chose to delete their comment? I hadn't noticed that until now). But since this seems to be a fix I think it's worthwhile leaving it up. – JuniorIncanter Jul 17 '14 at 16:50
-1

You are probably trying to run fragment activity on an emulator with Android version lower than HoneyComb 3.0. So for running fragment activity you have to have a phone or emualtor running Android 3.0 or later. You will have to add a supporting library android-support-v4.jar in your project. Once you have incorporated that library this issue will be gone.

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
  • 2
    Isn't the support library already there? You see it in the stack dump – talkol Jul 30 '13 at 23:22
  • As talkol noted, I already have the library included in my project. Further, I've been able to run this app before, so I'm not sure why this exception is suddenly popping up. – JuniorIncanter Jul 31 '13 at 00:08