由于:java.lang.NullPointerException:尝试在空对象引用上调用接口方法

4

我在向Android排行榜提交高分时遇到了问题。目前排行榜是空的,没有任何内容被提交。我有一个需要提交的整数"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);
        }
    }

日志记录:

造成的原因:java.lang.NullPointerException: 尝试在空对象引用上调用接口 方法'void com.google.android.gms.common.api.GoogleApiClient.connect()'

4个回答

11

这个陈述

mGoogleApiClient.connect();
mGoogleApiClient被实例化后应该出现。

当我这样做时,我会得到这个错误:"java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet." - l7ivine

9
您正在调用该方法。
connect()

关于对象的操作
mGoogleApiClient

不需要实例化它

你需要先写这个:

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

那么这个,

mGoogleApiClient.connect();

当我这样做时,我会得到这个错误:"java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet." - l7ivine
你正在使用 Google 登录, 你应该从 onStart() 方法中调用该方法。 - Sanjeet A
我刚刚创建了onStart()和onStop()方法,并在OnStart中放置了mGoogleApiClient.connect();,但我仍然收到相同的错误。 - l7ivine
当我删除“Games.Leaderboards.submitScoreImmediate(getApiClient(), String.valueOf(R.string.number_guesses_leaderboard), oldScore);”时,我的活动就可以正常工作。所以这就是我的错误所在。我应该如何更改以提交得分? - l7ivine

2

在初始化mGoogleApiClient引用之后,才能调用connect()。例如:

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

当我这样做时,我会得到这个错误:"java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet." - l7ivine
1
好的。听起来我们已经解决了你的“NullPointerException”。你对你的新问题做过任何研究吗? - Elliott Frisch

1
尝试这个过程: 在加载内容视图之前分配Google客户端:
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);
}

添加override onStart并连接Google客户端。

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接