简体   繁体   中英

Google+ button integration…NulPointerException

I have a problem with Google+ button integration in my app. I get an error, NullPointerException. I get the error on line:

mPlusOneButton.initialize("https://market.android.com/details?id=" + getPackageName(),

Here's my code:

public class PlusOne extends Activity implements ConnectionCallbacks,
        OnConnectionFailedListener {
    private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

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

    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        setContentView(R.layout.plus);
    }

    @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();
        }
    }

    @Override
    public void onDisconnected() {
    }

    public void onConnected() {

    }

    @Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub

    }
}

And LogCat:

02-02 19:08:04.713: E/AndroidRuntime(27086): FATAL EXCEPTION: main
02-02 19:08:04.713: E/AndroidRuntime(27086): Process: us.candycrushsaga.candycrushsagacompleteguide, PID: 27086
02-02 19:08:04.713: E/AndroidRuntime(27086): java.lang.RuntimeException: Unable to start activity ComponentInfo{us.candycrushsaga.candycrushsagacompleteguide/us.candycrushsaga.candycrushsagacompleteguide.PlusOne}: java.lang.NullPointerException
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.app.ActivityThread.access$800(ActivityThread.java:138)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.os.Looper.loop(Looper.java:136)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.app.ActivityThread.main(ActivityThread.java:5050)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at java.lang.reflect.Method.invoke(Method.java:515)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at dalvik.system.NativeStart.main(Native Method)
02-02 19:08:04.713: E/AndroidRuntime(27086): Caused by: java.lang.NullPointerException
02-02 19:08:04.713: E/AndroidRuntime(27086):    at us.candycrushsaga.candycrushsagacompleteguide.PlusOne.onCreate(PlusOne.java:39)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.app.Activity.performCreate(Activity.java:5241)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-02 19:08:04.713: E/AndroidRuntime(27086):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
02-02 19:08:04.713: E/AndroidRuntime(27086):    ... 11 more

Your mPlusOneButton now has null object. Assign an object to it.

PlusOneButton mPlusOneButton = new PlusOneButton(context); // here context is your activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM