引起:java.lang.NullPointerException: Attempt to invoke interface method on a null object reference

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28128946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:39:06  来源:igfitidea点击:

Caused by: java.lang.NullPointerException: Attempt to invoke interface method on a null object reference

javaandroidandroid-studionullpointerexception

提问by l7ivine

I am having trouble submitting the highscores to android leaderboards. Right now the leaderboards are empty and nothing is being submitted. I have an int "oldScore" that needs to be submitted. Right now I am trying a piece of code to submit it but the activity crashes when called. My code:

我无法将高分提交给 android 排行榜。现在排行榜是空的,没有提交任何内容。我有一个需要提交的 int“oldScore”。现在我正在尝试一段代码来提交它,但活动在调用时崩溃。我的代码:

public class GameOver extends BaseGameActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private GoogleApiClient mGoogleApiClient;
    public static int score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_over);

        mGoogleApiClient.connect();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Drive.API)
        .addScope(Drive.SCOPE_FILE)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

        int newScore = GameOver.score;

        SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);

        int oldScore = prefs.getInt("key", 0);
        if (newScore > oldScore) {
            SharedPreferences.Editor edit = prefs.edit();
            edit.putInt("key", newScore);
            edit.commit();

            EditText HighScore = (EditText) findViewById(R.id.HighScoreT);
            HighScore.setText("" + newScore);
        } else {
            EditText HighScore = (EditText) findViewById(R.id.HighScoreT);
            HighScore.setText("" + oldScore);
            Games.Leaderboards.submitScoreImmediate(getApiClient(),String.valueOf(R.string.number_guesses_leaderboard), oldScore);
        }
    }

Logcat:

日志猫:

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.android.gms.common.api.GoogleApiClient.connect()' on a null object reference

引起:java.lang.NullPointerException:尝试在空对象引用上调用接口方法“void com.google.android.gms.common.api.GoogleApiClient.connect()”

回答by Reimeus

The statement

该声明

mGoogleApiClient.connect();

should appear after mGoogleApiClienthas been instantiated

应该在mGoogleApiClient实例化后出现

回答by Sanjeet A

You are invoking the method

您正在调用该方法

connect()

on the object

在物体上

mGoogleApiClient

without instantiating it

没有实例化它

You need to write this first,

你需要先写这个,

  mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();  

then this,

那么这个,

mGoogleApiClient.connect();

回答by Elliott Frisch

You can't call connect()until afteryou initialize the mGoogleApiClientreference. Like,

你不能打电话connect(),直到您初始化mGoogleApiClient参考。喜欢,

// mGoogleApiClient.connect();
mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();       
mGoogleApiClient.connect(); // <-- move to here.

回答by Santhosh

Try this process : Assign google client before loading contentview:

试试这个过程:在加载内容视图之前分配谷歌客户端:

private GoogleApiClient mGoogleApiClient;
public static int score;

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

    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();

    setContentView(R.layout.activity_game_over);
}

Add override onStart and connect the google client.

添加覆盖 onStart 并连接谷歌客户端。

@Override
     public void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

@Override
public void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}