Google+按钮集成... NulPointerException

时间:2014-02-02 18:16:44

标签: java android

我的应用中的Google+按钮集成存在问题。我收到一个错误,NullPointerException。我在网上得到错误:

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

这是我的代码:

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

    }
}

和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

1 个答案:

答案 0 :(得分:1)

您的mPlusOneButton现在有null个对象。为其分配一个对象。

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