简体   繁体   English

在Android中获取Java Lang Null指针异常

[英]Getting Java Lang Null pointer exception in Android

I've been coding an android app and i've suddenly started getting an error, the error log says there is Java.Lang.Null.pointer.exception on line 23 of my Game.java yet none of the code concerning that has been changed. 我一直在编码一个Android应用程序,突然突然开始出现错误,错误日志显示Game.java的第23行有Java.Lang.Null.pointer.exception,但与此相关的代码都没有改变了。 Here is the code: Any ideas? 代码如下:有什么想法吗?

Main class 主班

public void onClick(View v) {
    switch (v.getId()) {        
    case R.id.about_button:
        Intent i = new Intent(this, About.class);
        startActivity(i);
        break;
    case R.id.ext_button:
        finish();
        break;

    case R.id.new_button:

        openNewGameDialog();
        break;
    }
}

private void openNewGameDialog() {

    new AlertDialog.Builder(this)

    .setTitle(R.string.diff_head)

    .setItems(R.array.difficulty,

     new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialoginterface, 
                int i) {

            startGame(i);

        }
    })

    .show();
}


private void startGame(int i) {

    Intent intent = new Intent(this, Game.class);

    intent.putExtra(Game.KEY_DIFFICULTY, i);

    startActivity(intent); 
}

Game.java Game.java

public class Game extends Activity {

public static final String KEY_DIFFICULTY = "w1279057.CW1.difficulty";
public static final int DIFFICULTY_NOVICE = 0;
public static final int DIFFICULTY_EASY = 1;
public static final int DIFFICULTY_MEDIUM = 2;
public static final int DIFFICULTY_GURU = 3;




            Random rand = new Random();


line 23 >>  int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE);

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

If that's the line where it's happening, then that means that the value being returned from getIntent() is null. 如果这是发生的行,则意味着从getIntent()返回的值为null。 This is why you should always avoid calling methods on the return results of method calls (see Law of Demeter ). 这就是为什么您应该始终避免在方法调用的返回结果上调用方法的原因(请参阅Demeter定律 )。

You should double check and see how this activity is getting started. 您应该仔细检查,看看该活动是如何开始的。 Chances are the troubles are there. 麻烦就在那儿。

You didn't upload enough code to make it clear.. But if I had to guess.. getIntent() will only return a non-null value if its being called from the startGame() method. 您没有上载足够的代码来清楚说明。.但是,如果我不得不猜测..如果从startGame()方法调用了getIntent(),则它只会返回一个非null值。 You will get a nullpointer if youre activity restarts (ie orientation change). 如果您的活动重新开始(即方向更改),您将获得一个空指针。 try something like this. 尝试这样的事情。

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState
    int diff;
    if(savedInstanceState == null)
         diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE);
}

and then if you want to save the state look at the method onRestoreInstanceState(Bundle outstate) 然后,如果要保存状态,请查看方法onRestoreInstanceState(Bundle outstate)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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