我正在尝试创建一个小部件,它每分钟都会更改文本,但我正在努力让事情发挥作用。我在标题中收到错误。
日志文件:
10-25 12:22:54.290: E/AndroidRuntime(21988): FATAL EXCEPTION: main
10-25 12:22:54.290: E/AndroidRuntime(21988): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.simpletext/com.example.simpletext.MainActivity}: java.lang.ClassCastException: com.example.simpletext.MainActivity cannot be cast to android.app.Activity
10-25 12:22:54.290: E/AndroidRuntime(21988): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
10-25 12:22:54.290: E/AndroidRuntime(21988): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
10-25 12:22:54.290: E/AndroidRuntime(21988): at android.app.ActivityThread.access$700(ActivityThread.java:140)
10-25 12:22:54.290: E/AndroidRuntime(21988): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
10-25 12:22:54.290: E/AndroidRuntime(21988): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 12:22:54.290: E/AndroidRuntime(21988): at android.os.Looper.loop(Looper.java:137)
10-25 12:22:54.290: E/AndroidRuntime(21988): at android.app.ActivityThread.main(ActivityThread.java:4921)
10-25 12:22:54.290: E/AndroidRuntime(21988): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 12:22:54.290: E/AndroidRuntime(21988): at java.lang.reflect.Method.invoke(Method.java:511)
10-25 12:22:54.290: E/AndroidRuntime(21988): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
10-25 12:22:54.290: E/AndroidRuntime(21988): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
10-25 12:22:54.290: E/AndroidRuntime(21988): at dalvik.system.NativeStart.main(Native Method)
10-25 12:22:54.290: E/AndroidRuntime(21988): Caused by: java.lang.ClassCastException: com.example.simpletext.MainActivity cannot be cast to android.app.Activity
10-25 12:22:54.290: E/AndroidRuntime(21988): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
10-25 12:22:54.290: E/AndroidRuntime(21988): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025)
10-25 12:22:54.290: E/AndroidRuntime(21988): ... 11 more
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simpletext"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="MyWidgetProvider"
android:icon="@drawable/ic_launcher"
android:label="Simple Text" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<activity
android:name="com.example.simpletext.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
类MainActivity,我试图每60秒更新一次小部件(是因为我没有任何onRecive方法吗? - 我在Updejt.class中设置了布局,这就是为什么我认为我不喜欢不需要一个):
package com.example.simpletext;
import java.util.Timer;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
public class MainActivity extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
Timer timer = new Timer();
timer.scheduleAtFixedRate(new UpdejtText(context, appWidgetManager), 1, 60000);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
UpdejtText类:
package com.example.simpletext;
import java.util.TimerTask;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;
public class UpdejtText extends TimerTask{
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
value = 0;
public UpdejtText(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
thisWidget = new ComponentName(context, UpdejtText.class);
value= someValue();
}
@Override
public void run(){
remoteViews.setTextViewText(R.id.textView1, Pomagac.getText1(value));
remoteViews.setTextViewText(R.id.textView2, Pomagac.getText2(value));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
帮助表示赞赏!
答案 0 :(得分:2)
因为您的MainActivity不会扩展Activity类。 相反,它扩展了AppWidgetProvider,
MainActivity extends AppWidgetProvider
检查此链接,在小部件的清单中注册您的课程,
答案 1 :(得分:1)
MainActivity
是一种类型AppWidgetProvider
,但有些人在你的班级中将它转换为普通的Android Activity
所以你得到了classcastException
..
答案 2 :(得分:1)
您不需要任何Activity
来处理widget.so您可以删除它。
将MainActivity.java
替换为MyWidgetProvider.java
package com.example.simpletext;
import java.util.Timer;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
public class MyWidgetProvider extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
Timer timer = new Timer();
timer.scheduleAtFixedRate(new UpdejtText(context, appWidgetManager), 1, 60000);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
并使用此清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simpletext"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".MyWidgetProvider"
android:icon="@drawable/ic_launcher"
android:label="Simple Text" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
</application>
</manifest>
注意:强> 要添加小部件,请长按主屏幕并添加所需的小部件。
我希望它会有所帮助!!