I'm trying to make an widget, which changes text every minute, but am struggling to get things working. I am getting the error in the Title.
Log file:
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
Manifest:
<?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>
And the classes MainActivity, where I'm trying to update the widget every 60s (is it because I don't have any onRecive method in it? - i set the layout in the Updejt.class thats why I assume I don't need one):
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);
}
}
the class 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);
}
}
Help appreciated!
Its because your MainActivity doesn't extend Activity class. Instead it extends AppWidgetProvider,
MainActivity extends AppWidgetProvider
Check this link on registering your class in manifest for widgets,
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments