问题描述
- 才刚刚开始学习安卓就出现问题,求解答
-
代码如下:package com.example.android_4_1;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;public class MainActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyView myView = new MyView(this); this.setContentView(myView); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
}
感觉代码没什么错,但是在模拟机上打开就 The application....has stopped unexpectedly!调试的时候会弹出找不到源。不知道是不是我环境没有搭建好。求帮忙~
08-12 14:39:12.440: D/AndroidRuntime(522): Shutting down VM
08-12 14:39:12.440: W/dalvikvm(522): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-12 14:39:12.490: E/AndroidRuntime(522): FATAL EXCEPTION: main
08-12 14:39:12.490: E/AndroidRuntime(522): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android_4_1/com.example.android_4_1.MainActivity}: java.lang.NullPointerException
08-12 14:39:12.490: E/AndroidRuntime(522): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-12 14:39:12.490: E/AndroidRuntime(522): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-12 14:39:12.490: E/AndroidRuntime(522): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-12 14:39:12.490: E/AndroidRuntime(522): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-12 14:39:12.490: E/AndroidRuntime(522): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 14:39:12.490: E/AndroidRuntime(522): at android.os.Looper.loop(Looper.java:123)
08-12 14:39:12.490: E/AndroidRuntime(522): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-12 14:39:12.490: E/AndroidRuntime(522): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 14:39:12.490: E/AndroidRuntime(522): at java.lang.reflect.Method.invoke(Method.java:507)
08-12 14:39:12.490: E/AndroidRuntime(522): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-12 14:39:12.490: E/AndroidRuntime(522): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-12 14:39:12.490: E/AndroidRuntime(522): at dalvik.system.NativeStart.main(Native Method)
08-12 14:39:12.490: E/AndroidRuntime(522): Caused by: java.lang.NullPointerException
08-12 14:39:12.490: E/AndroidRuntime(522): at com.example.android_4_1.MyView.(MyView.java:14)
08-12 14:39:12.490: E/AndroidRuntime(522): at com.example.android_4_1.MainActivity.onCreate(MainActivity.java:13)
08-12 14:39:12.490: E/AndroidRuntime(522): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-12 14:39:12.490: E/AndroidRuntime(522): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-12 14:39:12.490: E/AndroidRuntime(522): ... 11 more
解决方案
setContentView()调用2次了,把默认那个注释掉在试试
解决方案二:
setcontentview一般不像你这么用。。。一般默认的读一个布局就好了,把你自己写的控件放在布局上。。。不要像你写的那个new一个。。。这样子很容易出问题
解决方案三:
删掉“MyView myView = new MyView(this);
this.setContentView(myView);”
然后在R.layout.activity_main中随便添加一个控件,然后把控件名改为你的View的名字,注意要包含包名哦。
com.example.android_4_1.MyView
android:id="@+id/MyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
解决方案四:
Caused by: java.lang.NullPointerException
08-12 14:39:12.490: E/AndroidRuntime(522): at com.example.android_4_1.MyView.(MyView.java:14)
报了空指针异常,把这两句注释掉试试,怀疑是你的MyView是空的
MyView myView = new MyView(this);
this.setContentView(myView);
解决方案五:
如果环境没问题的话,把这两句注释掉试试
MyView myView = new MyView(this);
this.setContentView(myView);