Force close while calling mainactivity from widget (android)

Posted by Shaji Thorn Blue on Stack Overflow See other posts from Stack Overflow or by Shaji Thorn Blue
Published on 2013-11-04T09:49:53Z Indexed on 2013/11/04 9:53 UTC
Read the original article Hit count: 220

Filed under:
|

Iam creating a simple widget, by this widget i want to open my mainactivity.

Iam sending a unique key from my widget class to check whether my mainactivity is called via widget or not.

But as soon as i clicked on my widget my mainactivity get force close.

here is code of my widget class...

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] widgets) {
    // TODO Auto-generated method stub
    int numofWidgets = widgets.length;
    for(int i=0;i<numofWidgets;i++){
        int widget = widgets[i];
        Intent in = new Intent(context, EmergencyButton.class);
        in.putExtra("uniquevalue", "widget");
        PendingIntent pendingintent = PendingIntent.getActivity(context, 0, in, 0);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
        views.setOnClickPendingIntent(R.id.button, pendingintent);
        appWidgetManager.updateAppWidget(widget, views);
    }
}

And Here is my code of mainactivity where iam checking whether called came from widget or not

   @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainactivity); 
     Intent intentwidget = this.getIntent();
    if(intentwidget !=null)
    {
      String widgetdata = "nothing";
      widgetdata = intentwidget.getExtras().getString("uniquevalue");
      if(widgetdata.equals("widget"))
        {
          et1.setText(widgetdata);
        }
    }

}

And here is my logcat

11-04 14:57:14.361: E/AndroidRuntime(1701): FATAL EXCEPTION: main
11-04 14:57:14.361: E/AndroidRuntime(1701): java.lang.RuntimeException: Unable to start activityComponentInfo{com.appsionlabs.googlemapv2/com.appsionlabs.googlemapv2.EmergencyButton}: java.lang.NullPointerException
11-04 14:57:14.361: E/AndroidRuntime(1701):     at          android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at android.os.Looper.loop(Looper.java:123)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at java.lang.reflect.Method.invoke(Method.java:507)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at dalvik.system.NativeStart.main(Native Method)
11-04 14:57:14.361: E/AndroidRuntime(1701): Caused by: java.lang.NullPointerException
11-04 14:57:14.361: E/AndroidRuntime(1701):     at com.appsionlabs.googlemapv2.EmergencyButton.onCreate(EmergencyButton.java:29)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-04 14:57:14.361: E/AndroidRuntime(1701):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

© Stack Overflow or respective owner

Related posts about android

Related posts about widget