我的 Android 应用程序编译正常,但由于消息应用程序已停止而崩溃。我的代码有什么问题?
#GameView.java
public class GameView extends View {
public Bitmap droid;
public Matrix translate;
public GameView(Context context) {
super(context);
droid = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(droid, 10, 10, null);
}
#Game.java
public class Game extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout view = (FrameLayout)findViewById(R.id.graphics_holder);
view.addView(new GameView(this));
setContentView(R.layout.game);
日志猫
07-18 04:48:05.517: E/AndroidRuntime(960): FATAL EXCEPTION: main
07-18 04:48:05.517: E/AndroidRuntime(960): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oop/com.example.oop.Game}: java.lang.NullPointerException
07-18 04:48:05.517: E/AndroidRuntime(960): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-18 04:48:05.517: E/AndroidRuntime(960): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-18 04:48:05.517: E/AndroidRuntime(960): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-18 04:48:05.517: E/AndroidRuntime(960): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-18 04:48:05.517: E/AndroidRuntime(960): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 04:48:05.517: E/AndroidRuntime(960): at android.os.Looper.loop(Looper.java:137)
07-18 04:48:05.517: E/AndroidRuntime(960): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-18 04:48:05.517: E/AndroidRuntime(960): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 04:48:05.517: E/AndroidRuntime(960): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 04:48:05.517: E/AndroidRuntime(960): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-18 04:48:05.517: E/AndroidRuntime(960): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-18 04:48:05.517: E/AndroidRuntime(960): at dalvik.system.NativeStart.main(Native Method)
07-18 04:48:05.517: E/AndroidRuntime(960): Caused by: java.lang.NullPointerException
07-18 04:48:05.517: E/AndroidRuntime(960): at com.example.oop.Game.onCreate(Game.java:19)
07-18 04:48:05.517: E/AndroidRuntime(960): at android.app.Activity.performCreate(Activity.java:5008)
07-18 04:48:05.517: E/AndroidRuntime(960): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-18 04:48:05.517: E/AndroidRuntime(960): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-18 04:48:05.517: E/AndroidRuntime(960): ... 11 more
如果我改成这个,它可以工作
public class Game extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// FrameLayout view = (FrameLayout)findViewById(R.id.graphics_holder);
// view.addView();
setContentView(new GameView(this));
}
}
但我的意图是使用一个框架来封装我想要显示的内容,因为我的布局还有其他 GUI 对象要显示。
我的布局文件 game.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="@dimen/padding_large" >
<FrameLayout
android:id="@+id/graphics_holder"
android:layout_width="300dp"
android:layout_height="400dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</FrameLayout>
<TextView
android:id="@+id/bpm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="TextView"
android:textSize="@dimen/padding_large" />
</RelativeLayout>