TheDevMan
TheDevMan

Reputation: 5954

Using Bundle in an activity in Android

I have an intermittent issue in using bundle. I have created a widget and when the user clicks widget the widget opens the mainscreen and get the bundle value from there. This part is working fine without any issue.

The problem is when I open the mainActivity screen directly (By clicking the icon in the homescreen) I am getting a nullPointerException in the place where I have declared bundle. Once I get the error I am not able to open the app unless I force stop or clear the data.

The same code is working fine on Samsung S3 running 4.3, Nexus one, Galaxy Nexus too. I am getting this issue in KitKat 4.4.4..

I am not sure why this going inside the bundle when I haven't put anything in the bundle. Here is what I have done in MainActivity

 Bundle bundle =  getIntent().getExtras();
 if(bundle != null)  
 {
String callFromWidget = bundle.getString("callFromWidget");
Log.e("!null","Why is it coming inside the bundle " +callFromWidget);

if(callFromWidget.equalsIgnoreCase(res.getString(R.string.widget)))
{
      //do something
    }
 }
 else
 {

     //Update database there is nothing from bundle

 }

Here is the logs:

06-27 15:55:26.824: E/!null(7251): Why is it coming inside the bundle null
06-27 15:55:26.824: D/AndroidRuntime(7251): Shutting down VM
06-27 15:55:26.824: E/AndroidRuntime(7251): FATAL EXCEPTION: main
06-27 15:55:26.824: E/AndroidRuntime(7251): Process: com.vkv.ProjectB, PID: 7251
06-27 15:55:26.824: E/AndroidRuntime(7251): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vkv.ProjectB/com.vkv.ProjectB.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.os.Looper.loop(Looper.java:136)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at java.lang.reflect.Method.invoke(Native Method)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-27 15:55:26.824: E/AndroidRuntime(7251): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
06-27 15:55:26.824: E/AndroidRuntime(7251):     at com.vkv.ProjectB.Main.onCreate(Main.java:218)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.app.Activity.performCreate(Activity.java:5231)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-27 15:55:26.824: E/AndroidRuntime(7251):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-27 15:55:26.824: E/AndroidRuntime(7251):     ... 9 more

Upvotes: 0

Views: 481

Answers (2)

user2851150
user2851150

Reputation: 405

if(!TextUtils.isEmpty(callFromWidget)) {
    //    Apply your code
} 

Upvotes: 1

Steve Benett
Steve Benett

Reputation: 12933

If you call the Activity from the Widget you use the PendingIntent with your provided Bundle, which contains the desired String. But if you don't call the Activity this way another Intent is used, which doesn't contains the Bundle.

Hence, in this line

String callFromWidget = bundle.getString("callFromWidget");

null is passed to the String and that's why you get a NPE in your if-clause.

Check callFromWidget for null and the exception should be gone.

Bundle bundle =  getIntent().getExtras();
if(bundle != null) {

    String callFromWidget = bundle.getString("callFromWidget");
    Log.e("!null","Why is it coming inside the bundle " +callFromWidget);

    if(callFromWidget != null && callFromWidget.equalsIgnoreCase(res.getString(R.string.widget))) {
         //do something
    } else {
        //Update database there is nothing from bundle
    }
}

Upvotes: 0

Related Questions