Widget中的ClassCastException

时间:2012-05-04 11:36:15

标签: android

我开始在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)

它没有显示错误行,只是启动异常的类。而且我不知道为什么会发布此异常。任何帮助都将非常感激。

非常感谢!

3 个答案:

答案 0 :(得分:2)

您是否在清单中声明了Activity

你应该按照这样的方式切换onCreate()中的行序列:

super.onCreate(savedInstanceState);  // this should be first call
setContentView(R.layout.main);    
ImageButton boton=(ImageButton) findViewById(R.id.icono);

答案 1 :(得分:1)

我认为您的XML名称是“widget.xml”,但您在activity中调用main.xml。您必须更改SetContentView(R.layout.widget);

答案 2 :(得分:0)

某处,您正在致电startActivity()以启动WidgetAppProviderWidgetAppProviderBroadcastReceiver,而不是Activity