通过单击窗口小部件中的按钮,窗口小部件崩溃并无法启动新活动

时间:2013-09-30 07:18:09

标签: android widget

我的小部件包含文本视图和按钮。我想在用户点击窗口小部件中的按钮时启动新活动。

我的AppWidgetProvider类如下:

public class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub

        final int N = appWidgetIds.length;

        for(int i = 0; i<N; i++){

            int awID = appWidgetIds[i];

            Intent intent = new Intent(context, SettingsActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            views.setOnClickPendingIntent(R.id.btnSettings, pendingIntent);
//          v.setTextViewText(R.id.tvStatusDisplay, str);
            appWidgetManager.updateAppWidget(awID, views);
        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
        Toast.makeText(context, "Bye Bye...!!", Toast.LENGTH_SHORT).show();
    }   

}

我的小部件属性的xml文件位于:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_layout"
    android:minHeight="50dp"
    android:minWidth="200dp"
    android:updatePeriodMillis="60000" android:configure="com.example.widgetsample.WidgetConfigure">

</appwidget-provider>

我的清单文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.widgetsample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver
            android:name=".WidgetProvider" 
            android:label="@string/hello_widget" >

            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_stuff" />
        </receiver>

        <activity 
            android:name="com.example.widgetsample.settingsActivity"
            android:label="Settings" >                        
        </activity>

    </application>

</manifest>

帮助我在源代码中找到问题。 在此先感谢。

修改 的 我的logcat视图:

09-30 12:33:08.879: E/AndroidRuntime(278): FATAL EXCEPTION: main
09-30 12:33:08.879: E/AndroidRuntime(278): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9, result=-1, data=Intent { (has extras) }} to activity {com.sec.android.app.twlauncher/com.sec.android.app.twlauncher.Launcher}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.widgetsample/com.example.widgetsample.WidgetConfigure}; have you declared this activity in your AndroidManifest.xml?
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2590)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:2632)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2000(ActivityThread.java:117)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:130)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:3741)
09-30 12:33:08.879: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
09-30 12:33:08.879: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:507)
09-30 12:33:08.879: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-30 12:33:08.879: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-30 12:33:08.879: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
09-30 12:33:08.879: E/AndroidRuntime(278): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.widgetsample/com.example.widgetsample.WidgetConfigure}; have you declared this activity in your AndroidManifest.xml?
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.Activity.startActivityForResult(Activity.java:2827)
09-30 12:33:08.879: E/AndroidRuntime(278):  at com.sec.android.app.twlauncher.Launcher.startActivityForResult(Launcher.java:2209)
09-30 12:33:08.879: E/AndroidRuntime(278):  at com.sec.android.app.twlauncher.Launcher.addAppWidget(Launcher.java:2503)
09-30 12:33:08.879: E/AndroidRuntime(278):  at com.sec.android.app.twlauncher.Launcher.onActivityResult(Launcher.java:1073)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.Activity.dispatchActivityResult(Activity.java:3908)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:123)
09-30 12:33:08.879: E/AndroidRuntime(278):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2586)
09-30 12:33:08.879: E/AndroidRuntime(278):  ... 11 more
09-30 12:33:08.969: E/(183): Dumpstate > /data/log/dumpstate_app_error

1 个答案:

答案 0 :(得分:0)

您已在“appwidget-provider”中指定了您的活动,但您还必须将您的活动添加到清单文件,例如

<activity android:name="com.example.widgetsample.WidgetConfigure">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
    </intent-filter>
</activity>