我试图在我的应用程序中实现google+1按钮,但我得到了这个错误,在这一部分:

@Override
    protected void onStart() {
        super.onStart();
        mPlusClient.connect();
    }

在线:
mPlusClient.connect();

这是我的代码:
public class Menu extends Activity implements OnClickListener,ConnectionCallbacks,
OnConnectionFailedListener{

    ImageButton exit;

    private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

    private ProgressDialog mConnectionProgressDialog;
    private PlusClient mPlusClient;
    private ConnectionResult mConnectionResult;
    private PlusOneButton mPlusOneButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.menu);

        inicijalizujVars();

    }

    private void inicijalizujVars() {

        exit = (ImageButton) findViewById(R.id.imageButtonExit);

            mPlusOneButton = (PlusOneButton) findViewById(R.id.plus);

        exit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onResume(){
    super.onResume();

    mPlusClient = new PlusClient.Builder(this, this, this).clearScopes()
    .build();
  mPlusOneButton.initialize(mPlusClient,
    "https://market.android.com/details?id=" + getPackageName(),
    new OnPlusOneClickListener() {
        @Override
        public void onPlusOneClick(Intent intent) {
            mPlusOneButton.setVisibility(View.INVISIBLE);
            startActivityForResult(intent, 0);
        }
    });

    startAppAd.onResume();
    }


    @Override
    public void onBackPressed()
    {
    super.onBackPressed();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPlusClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPlusClient.disconnect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (mConnectionProgressDialog.isShowing()) {
            if (result.hasResolution()) {
                try {
                    result.startResolutionForResult(this,
                            REQUEST_CODE_RESOLVE_ERR);
                } catch (SendIntentException e) {
                    mPlusClient.connect();
                }
            }
        }
        mConnectionResult = result;
    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode,
            Intent intent) {
        if (requestCode == REQUEST_CODE_RESOLVE_ERR
                && responseCode == RESULT_OK) {
            mConnectionResult = null;
            mPlusClient.connect();
        }
    }

    public void onDisconnected() {
    }

    public void onConnected() {

    }
}

还有Logcat:
02-01 18:26:50.164: E/AndroidRuntime(2375): FATAL EXCEPTION: main
02-01 18:26:50.164: E/AndroidRuntime(2375): java.lang.RuntimeException: Unable to start activity ComponentInfo{us.candycrushsaga.candycrushsagacompleteguide/us.candycrushsaga.candycrushsagacompleteguide.Menu}: java.lang.NullPointerException
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.os.Looper.loop(Looper.java:137)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at java.lang.reflect.Method.invokeNative(Native Method)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at java.lang.reflect.Method.invoke(Method.java:511)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at dalvik.system.NativeStart.main(Native Method)
02-01 18:26:50.164: E/AndroidRuntime(2375): Caused by: java.lang.NullPointerException
02-01 18:26:50.164: E/AndroidRuntime(2375):     at us.candycrushsaga.candycrushsagacompleteguide.Menu.onStart(Menu.java:169)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.app.Activity.performStart(Activity.java:5018)
02-01 18:26:50.164: E/AndroidRuntime(2375):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
02-01 18:26:50.164: E/AndroidRuntime(2375):     ... 11 more

最佳答案

查看活动的生命周期
http://developer.android.com/training/basics/activity-lifecycle/starting.html
oncreate-->onstart-->onresume-->onpaused-->onstopped-->ondestored
mPlusClient.connect()中初始化时,调用onStart中的onResume。因此mPlusClient尚未初始化,导致NUllPointerException
因此,确保在mPlusClient之前初始化mPlusClient.connect()

08-04 03:39