从widget(android)调用mainactivity时强制关闭

时间:2013-11-04 09:49:53

标签: android widget

我创建了一个简单的小部件,通过这个小部件,我想打开我的主动作。

我从我的widget类中发送一个唯一的键来检查我的mainactivity是否通过widget调用。

但是当我点击我的小部件时,我的主动就会强行关闭。

这是我的widget类的代码......

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

这是我的主要活动代码,其中iam检查被叫是否来自小部件

   @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");//this is line 29 
      if(widgetdata.equals("widget"))
        {

        }
    }

}

这是我的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)

3 个答案:

答案 0 :(得分:2)

你有这个

 et1.setText(widgetdata);

et1未初始化。初始化et1

编辑:

使用以下内容并尝试

  Bundle extras = this.getIntent().getExtras();
  if (extras != null) {
        widgetdata = extras.getString("uniquevalue");
  }

答案 1 :(得分:1)

尝试以下操作,而不是Intent intentwidget = this.getIntent();

Bundle extras = this.getIntent().getExtras();

        if (extras != null) {
            widgetdata = extras.getString("uniquevalue");
        }

区别在于Bundle

答案 2 :(得分:0)

et1在该位置为空。

获取et1 id然后设置文本。