热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《引发者:java.lang.NullPointerException:尝试在null对象引用上调用接口方法》经验,为你挑选了2个好方法。

我无法将高分数提交给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:尝试在空对象引用上调用接口方法'void com.google.android.gms.common.api.GoogleApiClient.connect()'

Reimeus.. 11

该声明

mGoogleApiClient.connect();

应该在mGoogleApiClient实例化之后出现



1> Reimeus..:

该声明

mGoogleApiClient.connect();

应该在mGoogleApiClient实例化之后出现



2> Sanjeet A..:

您正在调用该方法

connect()

在对象上

mGoogleApiClient

没有实例化它

你需要先写这个,

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

那么这个,

mGoogleApiClient.connect();


推荐阅读
author-avatar
cfncjl_130
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有