1

我的小部件包含一个文本视图和一个按钮。当用户单击小部件中存在的按钮时,我想开始一个新活动。

我的 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
4

1 回答 1

0

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

<activity android:name="com.example.widgetsample.WidgetConfigure">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
    </intent-filter>
</activity>
于 2013-09-30T08:12:56.037 回答