简体   繁体   中英

Android Google + Sign In app imeadiatly stops working

What is wrong with my code? When I am trying to run app on device, it imeadiatly stops working and shows notification that app stopped working.

package lv.kvsol.atminivietu;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.plus.PlusClient;


public class MainActivity extends Activity implements View.OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener {
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewById(R.id.sign_in_button).setOnClickListener(this);
mPlusClient = new PlusClient.Builder(this, this, this)
        .setActions("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity")
        .setScopes(Scopes.PLUS_LOGIN)  // recommended login scope for social features
        // .setScopes("profile")       // alternative basic login scope
        .build();
// Progress bar to be displayed if the connection failure is not resolved.
mConnectionProgressDialog = new ProgressDialog(this);
mConnectionProgressDialog.setMessage("Signing in...");

}

@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()) {
// The user clicked the sign-in button already. Start to resolve
// connection errors. Wait until onConnected() to dismiss the
// connection dialog.
if (result.hasResolution()) {
  try {
           result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
   } catch (SendIntentException e) {
           mPlusClient.connect();
   }
}
}
// Save the result and resolve the connection failure upon a user click.
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 onConnected(Bundle connectionHint) {
String accountName = mPlusClient.getAccountName();
Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
}

@Override
public void onClick(View view) {
    if (view.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
        if (mConnectionResult == null) {
            mConnectionProgressDialog.show();
        } else {
            try {
                mConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                // Try connecting again.
                mConnectionResult = null;
                mPlusClient.connect();
            }
        }
    }
}

@Override
public void onDisconnected() {
Log.d(TAG, "disconnected");
}
}

There is my git repo: https://github.com/kvergins/AtminiVietu

There is log file:

01-20 21:38:30.350: W/asset(29993): Copying FileAsset 0x594481f0 (zip:/data/app/lv.kvsol.atminivietu-1.apk:/resources.arsc) to buffer size 142428 to make it aligned.
01-20 21:38:30.815: W/dalvikvm(29993): threadid=1: thread exiting with uncaught exception (group=0x41d67a08)
01-20 21:38:30.825: E/AndroidRuntime(29993): FATAL EXCEPTION: main
01-20 21:38:30.825: E/AndroidRuntime(29993): java.lang.RuntimeException: Unable to start activity ComponentInfo{lv.kvsol.atminivietu/lv.kvsol.atminivietu.MainActivity}: java.lang.NullPointerException
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.os.Looper.loop(Looper.java:158)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.app.ActivityThread.main(ActivityThread.java:5751)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at java.lang.reflect.Method.invoke(Method.java:511)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at dalvik.system.NativeStart.main(Native Method)
01-20 21:38:30.825: E/AndroidRuntime(29993): Caused by: java.lang.NullPointerException
01-20 21:38:30.825: E/AndroidRuntime(29993):    at lv.kvsol.atminivietu.MainActivity.onCreate(MainActivity.java:31)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.app.Activity.performCreate(Activity.java:5165)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
01-20 21:38:30.825: E/AndroidRuntime(29993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
01-20 21:38:30.825: E/AndroidRuntime(29993):    ... 11 more
01-20 21:38:30.910: D/Process(29993): killProcess, pid=29993
01-20 21:38:30.935: D/Process(29993): dalvik.system.VMStack.getThreadStackTrace(Native Method)
01-20 21:38:30.935: D/Process(29993): java.lang.Thread.getStackTrace(Thread.java:599)
01-20 21:38:30.935: D/Process(29993): android.os.Process.killProcess(Process.java:1004)
01-20 21:38:30.935: D/Process(29993): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:123)
01-20 21:38:30.935: D/Process(29993): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
01-20 21:38:30.935: D/Process(29993): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
01-20 21:38:30.935: D/Process(29993): dalvik.system.NativeStart.main(Native Method)

findViewById(R.id.sign_in_button) will return null, as you have not set your layout, using setContentView . That's why you get the NullpointerException in your logcat.

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