2 woouboy woouboy 于 2013.08.12 22:13 提问

才刚刚开始学习安卓就出现问题,求解答

代码如下: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

5个回答

shaojie519
shaojie519   2013.08.12 22:32
已采纳

setContentView()调用2次了,把默认那个注释掉在试试

shaojie519
shaojie519 空指针,如果你实在调不出来,加我qq1187044811
4 年多之前 回复
woouboy
woouboy 。。原来能继续编辑的~~- -,贴出来了,求大神帮我看看
4 年多之前 回复
woouboy
woouboy 我加你好友,你帮我看下吧。。现在好像已经贴不出那么大一段话了
4 年多之前 回复
shaojie519
shaojie519 Logcat日志?
4 年多之前 回复
woouboy
woouboy 还是一样。
4 年多之前 回复
tgbus18990140382
tgbus18990140382   2013.08.19 22:04

setcontentview一般不像你这么用。。。一般默认的读一个布局就好了,把你自己写的控件放在布局上。。。不要像你写的那个new一个。。。这样子很容易出问题

nonths
nonths   2013.08.20 09:46

删掉“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" />

qq446282412
qq446282412   2013.08.20 10:31

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);

xiaodi88061671
xiaodi88061671   2013.08.20 10:24

如果环境没问题的话,把这两句注释掉试试
MyView myView = new MyView(this);
this.setContentView(myView);

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!