为什么我得到这个NullPointErexception?

时间:2014-10-13 20:36:30

标签: android performance android-layout android-activity

我在我正在制作的应用程序的活动中做最简单的事情。在此活动中,我只是在用户单击按钮时打开活动。在运行时,应用程序崩溃,logcat显示存在'NullPointException'。无法弄清楚这个例外背后的原因。 日志显示

Caused by: java.lang.NullPointerException
        at com.example.lenovo.hitchhbo.GameOver.onCreate(GameOver.java:47)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)

我的活动是

public class GameOver extends Activity {

//TextView GO;
Button cont;
Intent startAgain;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    getActionBar().hide();

    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.INVISIBLE);





    cont=(Button)findViewById(R.id.button1);

    startAgain=new Intent(this,LauncherActivity2.class);

    cont.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cont.setTextColor(Color.argb(255,0,0,0));
            //overridePendingTransition(R.anim.transition1,R.anim.transition2);
            startActivity(startAgain);
        }
    });


    setContentView(R.layout.activity_game_over);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.game_over, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

根据Log,错误发生在

cont.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        cont.setTextColor(Color.argb(255,0,0,0));
        //overridePendingTransition(R.anim.transition1,R.anim.transition2);
        startActivity(startAgain);
    }
});

当我在行中初始化cont

时,这很疯狂
cont=(Button)findViewById(R.id.button1);

这有什么问题?

2 个答案:

答案 0 :(得分:2)

你必须致电

 setContentView(R.layout.activity_game_over);
在之前

你打电话

 cont=(Button)findViewById(R.id.button1);

原因是它在视图中搜索的按钮尚未存在。

答案 1 :(得分:0)

您还应初始化Intent对象startAgain。现在是null。见:

    cont=(Button)findViewById(R.id.button1);
//it is just a comment:
//startAgain=new Intent(this,LauncherActivity2.class);

cont.setOnClickListener(new View.OnClickListener() {
...

您应该删除此评论标记。