我写了一个简单的应用程序,开始时只包含一个 Activity 和多个其他类(不是从 Activity 继承的)。我现在添加了一个新类 HandlePrefs,它也继承自 Activity,并在与此类匹配的清单文件中添加了一个新标签。
现在我在启动期间遇到 java.lang.classcastexception 错误(但仅在使用 API lvl 15 及更高版本时。它在旧 API 版本上运行良好。
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tal.game.ammelampe.classes"
android:versionCode="3"
android:versionName="1.2"
android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<application android:icon="@drawable/icon"
android:label="Amme Lampe lite"
android:allowBackup="true">
<provider android:name=".FeedingProvider"
android:authorities="tal.game.ammelampe.FeedingProvider" />
<activity android:name="tal.game.ammelampe.classes.AmmeLampeApp"
android:label="Amme Lampe Lite"
android:configChanges="keyboard|keyboardHidden|orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="tal.game.ammelampe.classes.HandlePrefs">
</activity>
</application>
</manifest>
这是我的 AndroidApp 类(启动类)
package tal.game.framework.impl;
import tal.game.ammelampe.classes.HelpScreen;
import tal.game.ammelampe.classes.MainMenuScreen;
import tal.game.ammelampe.classes.Settings;
import tal.game.framework.App;
import tal.game.framework.FileIO;
import tal.game.framework.Graphics;
import tal.game.framework.Input;
import tal.game.framework.Screen;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.os.PowerManager.WakeLock;
//import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
public abstract class AndroidApp extends Activity implements App {
AndroidFastRenderView renderView;
Graphics graphics;
Input input;
FileIO fileIO;
Screen screen;
WakeLock wakeLock;
public static Vibrator v;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Debug.startMethodTracing();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
int frameBufferWidth = isLandscape ? 480 : 320;
int frameBufferHeight = isLandscape ? 320 : 480;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
frameBufferHeight, Config.RGB_565);
float scaleX = (float) frameBufferWidth / getWindowManager().getDefaultDisplay().getWidth();
float scaleY = (float) frameBufferHeight / getWindowManager().getDefaultDisplay().getHeight();
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
renderView = new AndroidFastRenderView(this, frameBuffer);
graphics = new AndroidGraphics(getAssets(), frameBuffer);
fileIO = new AndroidFileIO(getAssets());
input = new AndroidInput(this, renderView, scaleX, scaleY);
screen = getStartScreen();
setContentView(renderView);
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
}
同样继承自 Activity 的新类:
package tal.game.ammelampe.classes;
import tal.game.ammelampe.classes.FeedingDBMetaData.FeedingTableMetaData;
import tal.game.framework.App;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class HandlePrefs extends Activity{
// Definer keys i preferences
private static final String INITIALIZED = "initialized";
private static final String name = "name";
private static final String dob = "dob";
private static final String sex = "sex";
private static final String bgColor = "bgcolor";
private static final String onOffLamp = "onofflamp";
// This variable is set in the LoadingScreen class
public static Long currentDate;
public static Long LongDob;
public static String lName = "";
public static String lDob;
public static String lSex;
public static String lBgColor;
public static String lOnOffLamp;
static SharedPreferences myPrefs;
Activity activity = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
上面没有包含类中的很多代码,因为我相信它不会导致错误。但如果我错了,我当然会为你们提供它:)
最后是 LogCat 输出:
03-19 21:51:49.201: W/dalvikvm(3256): threadid=1: thread exiting with uncaught exception (group=0x40ab4228)
03-19 21:51:49.261: E/AndroidRuntime(3256): FATAL EXCEPTION: main
03-19 21:51:49.261: E/AndroidRuntime(3256): java.lang.RuntimeException: Unable to start activity ComponentInfo{tal.game.ammelampe.classes/tal.game.ammelampe.classes.AmmeLampeApp}: java.lang.ClassCastException: tal.game.ammelampe.classes.AmmeLampeApp cannot be cast to android.app.Application
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.app.ActivityThread.access$600(ActivityThread.java:139)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.os.Looper.loop(Looper.java:154)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.app.ActivityThread.main(ActivityThread.java:4945)
03-19 21:51:49.261: E/AndroidRuntime(3256): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 21:51:49.261: E/AndroidRuntime(3256): at java.lang.reflect.Method.invoke(Method.java:511)
03-19 21:51:49.261: E/AndroidRuntime(3256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-19 21:51:49.261: E/AndroidRuntime(3256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-19 21:51:49.261: E/AndroidRuntime(3256): at dalvik.system.NativeStart.main(Native Method)
03-19 21:51:49.261: E/AndroidRuntime(3256): Caused by: java.lang.ClassCastException: tal.game.ammelampe.classes.AmmeLampeApp cannot be cast to android.app.Application
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.view.Window.getCompatInfo(Window.java:475)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.view.Window$LocalWindowManager.<init>(Window.java:485)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.view.Window.setWindowManager(Window.java:471)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.app.Activity.attach(Activity.java:4517)
03-19 21:51:49.261: E/AndroidRuntime(3256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2135)
03-19 21:51:49.261: E/AndroidRuntime(3256): ... 11 more
03-19 21:51:49.281: E/EmbeddedLogger(185): App crashed! Process: tal.game.ammelampe.classes
03-19 21:51:49.281: E/EmbeddedLogger(185): App crashed! Package: tal.game.ammelampe.classes v3 (1.2)
03-19 21:51:49.281: E/EmbeddedLogger(185): App crashed! Application Label: Amme Lampe lite
03-19 21:51:49.281: W/ActivityManager(185): Force finishing activity tal.game.ammelampe.classes/.AmmeLampeApp
我尝试使用 android:name="android.app.Application" 标记作为标记中的直接子项修改清单文件(如其他类似问题中所建议的那样),但仍然遇到相同的错误。我在这里做错了什么?
清单文件中带有 android:name="android.app.Application" 标记的 LogCat:
03-19 22:00:19.789: E/AndroidRuntime(4154): FATAL EXCEPTION: main
03-19 22:00:19.789: E/AndroidRuntime(4154): java.lang.RuntimeException: Unable to start activity ComponentInfo{tal.game.ammelampe.classes/tal.game.ammelampe.classes.AmmeLampeApp}: java.lang.ClassCastException: tal.game.ammelampe.classes.AmmeLampeApp cannot be cast to android.app.Application
应用类:
package tal.game.framework;
import android.content.Context;
public interface App {
public Input getInput();
public FileIO getFileIO();
public Graphics getGraphics();
public void setScreen(Screen screen);
public Screen getCurrentScreen();
public Screen getStartScreen();
public Context getApplicationContext();
}