简体   繁体   中英

Why am I getting this NullPointException?

I am doing the simplest thing in an activity of an application I am making. In this activity I am just opening an activity when a user clicks a button. At Runtime the application crashes and the logcat shows that there is a 'NullPointException'. Cannot Figure out the reason behind this Exception. log shows

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)

and my Activity is

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

According to Log the error is at

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

This is crazy when I have initialized cont in line

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

What can be the problem with this?

You have to call

 setContentView(R.layout.activity_game_over);

before you call

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

The reason for this is that it's searching for a button in a view that doesn't exist yet.

And also you should initialize your Intent object startAgain . It is null now. See that:

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

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

You should remove this comment tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM