我从 android 世界的小部件开始。我有这个布局,名为“widget.xml”:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >
<ImageButton
android:id="@+id/icono"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
如您所见,它只是一个按钮。这是小部件的配置 xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minWidth="72dip"
android:minHeight="72dip"
android:updatePeriodMillis="7000"
android:label="Widget_azafata"
/>
这是扩展 AppWidgetProvider 的类,它没有被改动:
public class WidgetAppProvider extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
这是配置活动:
public class WidgetConfig extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
ImageButton boton=(ImageButton) findViewById(R.id.icono);
//stuff here
}
清单中的行:
<receiver
android:name=".WidgetAppProvider"
android:label="Widget_azafata">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_azafata_xml" />
</receiver>
好吧,我启动小部件并显示此异常:
05-04 10:21:14.144: E/AndroidRuntime(320): FATAL EXCEPTION: main
05-04 10:21:14.144: E/AndroidRuntime(320): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.publidirecta.Azafata/com.publidirecta.Azafata.WidgetAppProvider}: java.lang.ClassCastException: com.publidirecta.Azafata.WidgetAppProvider
05-04 10:21:14.144: E/AndroidRuntime(320): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-04 10:21:14.144: E/AndroidRuntime(320): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-04 10:21:14.144: E/AndroidRuntime(320): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-04 10:21:14.144: E/AndroidRuntime(320): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-04 10:21:14.144: E/AndroidRuntime(320): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 10:21:14.144: E/AndroidRuntime(320): at android.os.Looper.loop(Looper.java:123)
05-04 10:21:14.144: E/AndroidRuntime(320): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-04 10:21:14.144: E/AndroidRuntime(320): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 10:21:14.144: E/AndroidRuntime(320): at java.lang.reflect.Method.invoke(Method.java:521)
05-04 10:21:14.144: E/AndroidRuntime(320): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-04 10:21:14.144: E/AndroidRuntime(320): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-04 10:21:14.144: E/AndroidRuntime(320): at dalvik.system.NativeStart.main(Native Method)
05-04 10:21:14.144: E/AndroidRuntime(320): Caused by: java.lang.ClassCastException: com.publidirecta.Azafata.WidgetAppProvider
05-04 10:21:14.144: E/AndroidRuntime(320): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-04 10:21:14.144: E/AndroidRuntime(320): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
它没有显示任何错误行,只是启动异常的类。而且我不知道为什么会启动此异常。任何帮助将不胜感激。
非常感谢!