下面我向您展示了Manifest.Xml,MainActivityClass并添加了Logcat错误。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lockscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission
android:name="android.permission.WRITE_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.lockscreen.LockControlWidgetProvider" />
<activity
android:name="com.example.lockscreen.LockMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.lockscreen.LockControlService"
android:label="@string/title_activity_lock_control_service" >
</activity>
<service
android:enabled="true"
android:name="LockControlService"
></service>
</application>
</manifest>
我的代码如下:
公共类LockMainActivity扩展了AppWidgetProvider { @覆盖 public void onUpdate(Context context,AppWidgetManager appWidgetManager,int [] appWidgetIds) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.lock_main); final Intent unlockIntent = createIntent(context,appWidgetIds); unlockIntent.putExtra(LockControlWidgetProvider.EXTRA_LOCK_ACTIVATED,false); unlockIntent.setAction(LockControlWidgetProvider.ACTION_UNLOCK); final PendingIntent pendingUnlockIntent = createPendingIntent(context,unlockIntent);
// Lock Intent
final Intent lockIntent = createIntent(context, appWidgetIds);
lockIntent.putExtra(LockControlWidgetProvider.EXTRA_LOCK_ACTIVATED, true);
lockIntent.setAction(LockControlWidgetProvider.ACTION_LOCK);
final PendingIntent pendingLockIntent = createPendingIntent(context, lockIntent);
// Status Intent
final Intent statusIntent = createIntent(context, appWidgetIds);
statusIntent.setAction(LockControlWidgetProvider.ACTION_LOCK);
final PendingIntent pendingStatusIntent = createPendingIntent(context, statusIntent);
//bind click
remoteViews.setOnClickPendingIntent(R.id.txtOn, pendingLockIntent);
remoteViews.setOnClickPendingIntent(R.id.txtOff, pendingUnlockIntent);
remoteViews.setOnClickPendingIntent(R.id.widget_root, pendingStatusIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
context.startService(statusIntent);
}
private Intent createIntent(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
Intent updateIntent = new Intent(context.getApplicationContext(),
LockControlWidgetProvider.class);
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
appWidgetIds);
return updateIntent;
}
private PendingIntent createPendingIntent(Context context,
Intent updateIntent) {
// TODO Auto-generated method stub
PendingIntent pendingIntent = PendingIntent.getService(
context.getApplicationContext(), 0, updateIntent,PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
}
错误如下:
07-18 20:48:45.500: E/AndroidRuntime(2104): FATAL EXCEPTION: main
07-18 20:48:45.500: E/AndroidRuntime(2104): Process: com.example.lockscreen, PID: 2104
07-18 20:48:45.500: E/AndroidRuntime(2104): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lockscreen/com.example.lockscreen.LockMainActivity}: java.lang.ClassCastException: com.example.lockscreen.LockMainActivity cannot be cast to android.app.Activity
07-18 20:48:45.500: E/AndroidRuntime(2104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-18 20:48:45.500: E/AndroidRuntime(2104): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-18 20:48:45.500: E/AndroidRuntime(2104): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-18 20:48:45.500: E/AndroidRuntime(2104): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-18 20:48:45.500: E/AndroidRuntime(2104): at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 20:48:45.500: E/AndroidRuntime(2104): at android.os.Looper.loop(Looper.java:136)
07-18 20:48:45.500: E/AndroidRuntime(2104): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-18 20:48:45.500: E/AndroidRuntime(2104): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 20:48:45.500: E/AndroidRuntime(2104): at java.lang.reflect.Method.invoke(Method.java:515)
07-18 20:48:45.500: E/AndroidRuntime(2104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-18 20:48:45.500: E/AndroidRuntime(2104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-18 20:48:45.500: E/AndroidRuntime(2104): at dalvik.system.NativeStart.main(Native Method)
07-18 20:48:45.500: E/AndroidRuntime(2104): Caused by: java.lang.ClassCastException: com.example.lockscreen.LockMainActivity cannot be cast to android.app.Activity
07-18 20:48:45.500: E/AndroidRuntime(2104): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-18 20:48:45.500: E/AndroidRuntime(2104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-18 20:48:45.500: E/AndroidRuntime(2104): ... 11 more
07-18 20:53:46.984: I/Process(2104): Sending signal. PID: 2104 SIG: 9
答案 0 :(得分:0)
发生此错误是因为LockMainActivity
不是Activity
。您不能将其称为活动 - 它需要扩展Activity
类。
在您的清单(在<activity>
标记中)或在另一个Activity(使用Intent)中的某处,您尝试将LockMainActivity
用作活动,因此应用程序崩溃。