引发者:java.lang.NullPointerException:尝试在null对象引用上调用接口方法

时间:2015-01-24 18:42:47

标签: java android android-studio nullpointerexception

我无法将高分数提交到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的:

  

引起:java.lang.NullPointerException:尝试调用接口   方法'无效   com.google.android.gms.common.api.GoogleApiClient.connect()'上的null   对象参考

4 个答案:

答案 0 :(得分:10)

声明

mGoogleApiClient.connect();

应在mGoogleApiClient实例化后出现

答案 1 :(得分:9)

您正在调用方法

connect()
对象上的

mGoogleApiClient

没有实例化

你需要先写这个,

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

那么,

mGoogleApiClient.connect();

答案 2 :(得分:2)

在初始化connect()引用之后, {em>}之后才能调用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.

答案 3 :(得分: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);
}

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

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

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