Question

I'm trying to implement Google +1 button in my app but I get this error, in this part:

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

On line:

mPlusClient.connect();

Here's my code:

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() {

    }
}

And 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
Était-ce utile?

La solution

Look at the lifecycle of the activity

http://developer.android.com/training/basics/activity-lifecycle/starting.html

onCreate --> onStart--> onResume--> onPaused--> onStopped--> onDestoryed

You call mPlusClient.connect() in onStart while you initialize it in onResume. So mPlusClient is not yet initialized leading to NUllPointerException.

So make sure you initialize mPlusClient before mPlusClient.connect()

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top