2013-09-30 51 views
1

我的小部件包含一个textview和一个按钮。我想在用户点击小部件中的按钮时开始新的活动。小部件崩溃,无法通过点击小部件中的按钮来启动新活动

我的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 
+0

请问您可以粘贴您的崩溃日志吗? –

回答

0

您指定的“appwidget提供商”的活动,但你还必须添加您的活动清单文件,例如

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

但我没有使用任何活动的小部件配置... –

+0

然后,你必须摆脱android:configure =“com.example.widgetsample.WidgetConfigure”,因为它实际上指定你想使用WidgetConfigure活动。 – interrupt

+0

嘿哥们删除后 android:configure =“com.example.widgetsample.WidgetConfigure”。 我现在能够加载小部件,但点击小部件上的设置按钮,它不会启动SettingsActivity –

相关问题