传递失败导致窗口小部件配置活动

时间:2011-03-09 12:54:35

标签: android configuration android-activity widget

我正在尝试创建一个小部件,如果我在配置活动中取消(按回),我的小部件中会出现以下错误:

03-09 12:34:47.650: ERROR/AndroidRuntime(837): FATAL EXCEPTION: main
03-09 12:34:47.650: ERROR/AndroidRuntime(837): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=5, result=-1, data=Intent { act=CANCELED (has extras) }} to activity {com.android.launcher/com.android.launcher2.Launcher}: java.lang.NullPointerException
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at android.app.ActivityThread.access$2800(ActivityThread.java:125)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at android.os.Looper.loop(Looper.java:123)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at java.lang.reflect.Method.invokeNative(Native Method)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at java.lang.reflect.Method.invoke(Method.java:521)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at dalvik.system.NativeStart.main(Native Method)
03-09 12:34:47.650: ERROR/AndroidRuntime(837): Caused by: java.lang.NullPointerException
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at com.android.launcher2.Launcher.completeAddAppWidget(Launcher.java:889)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at com.android.launcher2.Launcher.onActivityResult(Launcher.java:542)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at android.app.Activity.dispatchActivityResult(Activity.java:3890)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
03-09 12:34:47.650: ERROR/AndroidRuntime(837):     ... 11 more

我不知道为什么会这样。

在我的AndroidManifest.xml中,我做了所有类似于android小部件教程中描述的内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="eu.scilled.rss"
    android:versionCode="1"
    android:versionName="1.0"
>
    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:debuggable="true">
        <receiver android:name=".RSSWidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"
                ></action>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widget_provider_metadata"
            ></meta-data>
        </receiver>
        <activity android:name=".activity.ConfigActivity">
            <intent-filter>
                <action yndroid:name="android.appwidget.action.APPWIDGET_CONFIGURE"></action>
            </intent-filter>
        </activity>
    </application>
</manifest>

和我的widget_provider_metadata.xml:

<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:updatePeriodMillis="86400000"
    android:minHeight="72dp"
    android:initialLayout="@layout/widget_layout"
    android:minWidth="146dp" android:configure="eu.scilled.rss.activity.ConfigActivity">
</appwidget-provider>

问题是,为什么会出现此错误,以及为什么我的窗口小部件提供程序无法对从配置活动触发的ResultIntent做出反应。

活动来源:

public class ConfigActivity extends Activity implements OnClickListener {
    private static final String TAG = (ConfigActivity.class).getCanonicalName();
    private int THIS_WIDGET_ID = AppWidgetManager.INVALID_APPWIDGET_ID;
    private Intent resultIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "starting configuration activity");
        setContentView(R.layout.config_activity_layout);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            THIS_WIDGET_ID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        resultIntent = new Intent();
        resultIntent.putExtra("EXTRA_APPWIDGET_ID", THIS_WIDGET_ID);
        createResultIntent(false);
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(this);
    }

    private void createResultIntent(boolean finished) {
        if (!finished) {
            resultIntent.setAction("CANCELED");
            setResult(RESULT_OK, resultIntent);
        } else {
            resultIntent.setAction("CONFIG_DONE");
            setResult(RESULT_CANCELED, resultIntent);
        }
    }

    @Override
    public void onClick(View v) {
        createResultIntent(true);
        finish();

    }

    @Override
    public void onBackPressed() {

        createResultIntent(false);
        finish();

    }
}

2 个答案:

答案 0 :(得分:1)

至少,你在createResultIntent()中弄乱了一些东西:按下BACK按钮时返回RESULT_OK,而不是RESULT_CANCELED。我认为您应该根据this SDK example删除resultIntent.setAction("XXX")

答案 1 :(得分:0)

在onBackPressed()中执行此操作有什么问题? AppWidgetConfig-activity的自然适合是PreferencesActivity,并且一个IS使用后退按钮。