我是 android 的初学者,我相信我的代码一切正常,但是在模拟器中加载应用程序时,应用程序崩溃并且需要强制关闭。我得到了下面的 logcat,我搜索了错误,但老实说,我不太了解将他们的其他解决方案转换为适合我的解决方案(我确实尝试过)
编辑:从我可以从其他线程中得知,我可能需要在清单中添加一些东西?
有人可以用下面的 logcat 向我解释我应该在我的代码中寻找什么,或者至少指出我可以尝试的东西吗?
如果我还能做些什么来帮助解决这个问题,我非常感激它接缝我的很多项目都受到同样错误的困扰。
提前感谢所有帮助,这是我的第一篇文章,所以很好:)
日志猫
10-22 20:59:20.258: D/AndroidRuntime(307): Shutting down VM
10-22 20:59:20.258: W/dalvikvm(307): threadid=1: thread exiting with uncaught
exception (group=0x4001d800)
10-22 20:59:20.348: E/AndroidRuntime(307): FATAL EXCEPTION: main
10-22 20:59:20.348: E/AndroidRuntime(307): java.lang.RuntimeException: Unable to
instantiate activity
ComponentInfo{com.example.w1_t2_surfaceview/com.example.w1_t2_surfaceview.MyGame}:
java.lang.ClassNotFoundException: com.example.w1_t2_surfaceview.MyGame in loader
dalvik.system.PathClassLoader[/data/app/com.example.w1_t2_surfaceview-2.apk]
10-22 20:59:20.348: E/AndroidRuntime(307): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
10-22 20:59:20.348: E/AndroidRuntime(307): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-22 20:59:20.348: E/AndroidRuntime(307): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-22 20:59:20.348: E/AndroidRuntime(307): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-22 20:59:20.348: E/AndroidRuntime(307): at
android.os.Handler.dispatchMessage(Handler.java:99)
10-22 20:59:20.348: E/AndroidRuntime(307): at android.os.Looper.loop(Looper.java:123)
10-22 20:59:20.348: E/AndroidRuntime(307): at
android.app.ActivityThread.main(ActivityThread.java:4627)
10-22 20:59:20.348: E/AndroidRuntime(307): at
java.lang.reflect.Method.invokeNative(Native Method)
10-22 20:59:20.348: E/AndroidRuntime(307): at
java.lang.reflect.Method.invoke(Method.java:521)
10-22 20:59:20.348: E/AndroidRuntime(307): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-22 20:59:20.348: E/AndroidRuntime(307): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-22 20:59:20.348: E/AndroidRuntime(307): at dalvik.system.NativeStart.main(Native
Method)
10-22 20:59:20.348: E/AndroidRuntime(307): Caused by:
java.lang.ClassNotFoundException: com.example.w1_t2_surfaceview.MyGame in loader
dalvik.system.PathClassLoader[/data/app/com.example.w1_t2_surfaceview-2.apk]
10-22 20:59:20.348: E/AndroidRuntime(307): at
dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
10-22 20:59:20.348: E/AndroidRuntime(307): at
java.lang.ClassLoader.loadClass(ClassLoader.java:573)
10-22 20:59:20.348: E/AndroidRuntime(307): at
java.lang.ClassLoader.loadClass(ClassLoader.java:532)
10-22 20:59:20.348: E/AndroidRuntime(307): at
android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-22 20:59:20.348: E/AndroidRuntime(307): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
10-22 20:59:20.348: E/AndroidRuntime(307): ... 11 more
MyGame类包com.example.game;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MyGame extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
}
}
Gameview类包com.example.game;导入 com.example.w1_t2_surfaceview.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceView;
public class GameView extends SurfaceView {
private Bitmap bmp;
public GameView(Context context) {
super(context);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.CYAN);
canvas.drawBitmap(bmp, 10, 10, null);
}
}
显现
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name=".MyGame"
android:label="@string/title_surfaceview" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
字符串.XML
<resources>
<string name="app_name">W1_T2_SurfaceView</string>
<string name="title_surfaceview">Surface View 2</string>
</resources>
由于开放 1 周而导致线程颠簸。