java.lang.RuntimeException错误 求帮助

wjffttfx 2014-05-09 05:22:32
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/eatId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃饭" />
<CheckBox
android:id="@+id/sleepId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉" />
<CheckBox
android:id="@+id/dota"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dota" />
</LinearLayout>

MainActivity.java

package com.xxm.view.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;

public class MainActivity extends Activity {

private CheckBox eatBox;
private CheckBox sleepBox;
private CheckBox dotaBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//加上下面的就出错
eatBox=(CheckBox) eatBox.findViewById(R.id.eatId);
sleepBox=(CheckBox) sleepBox.findViewById(R.id.sleepId);
dotaBox=(CheckBox) dotaBox.findViewById(R.id.dota);

}

@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;
}

}


LogCat
05-09 09:14:21.406: E/Trace(1204): error opening trace file: No such file or directory (2)
05-09 09:14:22.356: D/AndroidRuntime(1204): Shutting down VM
05-09 09:14:22.356: W/dalvikvm(1204): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-09 09:14:22.377: E/AndroidRuntime(1204): FATAL EXCEPTION: main
05-09 09:14:22.377: E/AndroidRuntime(1204): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxm.view.checkbox/com.xxm.view.checkbox.MainActivity}: java.lang.NullPointerException
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.os.Looper.loop(Looper.java:137)
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-09 09:14:22.377: E/AndroidRuntime(1204): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 09:14:22.377: E/AndroidRuntime(1204): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 09:14:22.377: E/AndroidRuntime(1204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-09 09:14:22.377: E/AndroidRuntime(1204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-09 09:14:22.377: E/AndroidRuntime(1204): at dalvik.system.NativeStart.main(Native Method)
05-09 09:14:22.377: E/AndroidRuntime(1204): Caused by: java.lang.NullPointerException
05-09 09:14:22.377: E/AndroidRuntime(1204): at com.xxm.view.checkbox.MainActivity.onCreate(MainActivity.java:19)
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.app.Activity.performCreate(Activity.java:5104)
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-09 09:14:22.377: E/AndroidRuntime(1204): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-09 09:14:22.377: E/AndroidRuntime(1204): ... 11 more
05-09 09:19:22.476: I/Process(1204): Sending signal. PID: 1204 SIG: 9
...全文
1387 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsp_hjg 2014-05-10
  • 打赏
  • 举报
回复
是this.findViewById();
wjffttfx 2014-05-09
  • 打赏
  • 举报
回复
菜鸟的封装史 2014-05-09
  • 打赏
  • 举报
回复
Caused by: java.lang.NullPointerException空指针异常,看异常一般直接找Caused by,要是没有再从头看。至于解决办法楼上已经给出咯: eatBox=(CheckBox) eatBox.findViewById(R.id.eatId); sleepBox=(CheckBox) sleepBox.findViewById(R.id.sleepId); dotaBox=(CheckBox) dotaBox.findViewById(R.id.dota); 改为: eatBox=(CheckBox) findViewById(R.id.eatId); sleepBox=(CheckBox) findViewById(R.id.sleepId); dotaBox=(CheckBox) findViewById(R.id.dota);--------这里也可以是this.findViewById(R.id.dota);this代表当前activity,你的代码是自己刚定义的view,首先不说可不可以,首先他们都没有初始化对象为null,所以会报出异常。第二就算不为空也不能这样做因为在你自己定义的对象中就不包含你需要findViewById的对象,所以最后也会是null,对象初始化失败。
乐逍遥二 2014-05-09
  • 打赏
  • 举报
回复
eatBox=(CheckBox) eatBox.findViewById(R.id.eatId); sleepBox=(CheckBox) sleepBox.findViewById(R.id.sleepId); dotaBox=(CheckBox) dotaBox.findViewById(R.id.dota); 看清楚了java.lang.NullPointerException id木有取到, eatBox=(CheckBox) eatBox.findViewById(R.id.eatId); sleepBox=(CheckBox) sleepBox.findViewById(R.id.sleepId); dotaBox=(CheckBox) dotaBox.findViewById(R.id.dota); 改为: eatBox=(CheckBox) findViewById(R.id.eatId); sleepBox=(CheckBox) findViewById(R.id.sleepId); dotaBox=(CheckBox) findViewById(R.id.dota);

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧