2014-01-20 197 views
0

我的代碼出了什麼問題?當我嘗試在設備上運行應用程序時,它imeadiatly停止工作並顯示應用程序停止工作的通知。Android Google +登錄app imeadiatly停止工作

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"); 
} 
} 

還有就是我的混帳回購協議:https://github.com/kvergins/AtminiVietu

有日誌文件:

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) 
+0

在這裏發佈日誌 – Android

+0

LogCat在哪裏? –

回答

2

findViewById(R.id.sign_in_button)將返回null,因爲你沒有設置你的佈局,用setContentView。這就是爲什麼你在logcat中得到NullpointerException。