活动不是出于未知原因

时间:2014-03-23 17:35:32

标签: android nullpointerexception android-activity

logcat的:

03-24 00:27:31.156: E/AndroidRuntime(13836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxxx.yyyyyy/com.xxxxxx.yyyyyy.Main}: java.lang.NullPointerException

这是我在尝试启动应用程序时在logCat中看到的内容。但在我看来我的代码还行,所以我不知道问题是什么,请帮助!提前谢谢。

Main.java

package com.xxxxxx.yyyyyy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;  



public class Main extends Activity
{

Button start;

protected void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(0x7f030010);
    start = (Button)findViewById(0x7f080047);
    start.setOnClickListener(new android.view.View.OnClickListener() {



        public void onClick(View view)
        {
            Intent intent = new Intent(Main.this, Tab1.class);
            startActivity(intent);
        }

    });
}


}

2 个答案:

答案 0 :(得分:4)

为什么硬编码值?没必要。

当你在布局中添加一个带有id的按钮时,R.java会自动输入。初始化视图时,只需引用id。

只需确保您的布局有一个按钮,您只需在活动中引用它

setContentView(R.layout.yourlayout);
start = (Button)findViewById(R.id.startbutton);

Tab1中的某些内容也可能为空。

答案 1 :(得分:0)

很可能你的start变量为null。使用Raghunandan描述的资源来检索Button引用。

相关问题