1

我想實現谷歌的登錄按鈕到我的應用程序,雖然它最初運行時,我收到此錯誤:谷歌登錄按鈕空指針異常

09-16 19:44:38.679 26125-26125/com.hudsoncorp.zahudson.caravan E/AndroidRuntime? FATAL EXCEPTION: main 
    Process: com.hudsoncorp.zahudson.caravan, PID: 26125 
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference 
      at com.hudsoncorp.zahudson.caravan.HomeActivity.onSignInClicked(HomeActivity.java:189) 
      at com.hudsoncorp.zahudson.caravan.HomeActivity.onClick(HomeActivity.java:177) 
      at com.google.android.gms.common.SignInButton.onClick(Unknown Source) 
      at android.view.View.performClick(View.java:5156) 
      at android.view.View$PerformClick.run(View.java:20755) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:145) 
      at android.app.ActivityThread.main(ActivityThread.java:5835) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

我只是修正了一些實現方法與谷歌登錄並遇到這個問題。我對Android相當陌生,尤其是在實現GoogleApiClient的時候。我想知道我的錯誤與我的方法的順序有什麼關係。然後,我再也不知道我在說什麼了。我的主頁活動代碼是:

import android.app.Activity; import android.content.Intent; import android.content.IntentSender; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; 


import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.Scopes; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; import com.google.android.gms.common.api.Scope; import com.google.android.gms.plus.Plus; import static android.view.View.*; 


public class HomeActivity extends Activity implements 
     ConnectionCallbacks, 
     OnConnectionFailedListener, 
     OnClickListener { 

    private static final int RC_SIGN_IN = 0; 
    private GoogleApiClient mGoogleApiClient; 
    private Button mLoginButton; 
    private TextView mSignUpLabel; 

    /* Is there a ConnectionResult resolution in progress? */ 
    private boolean mIsResolving = false; 

    /* Should we automatically resolve ConnectionResults when possible? */ 
    private boolean mShouldResolve = false; 

    public static final String TAG = HomeActivity.class.getSimpleName(); 
    private TextView mStatusTextView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 

     mLoginButton = (Button) findViewById(R.id.btnLogin); 
     mLoginButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(HomeActivity.this, "Button Pressed", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     mSignUpLabel = (TextView) findViewById(R.id.lblAboutUs); 
     mSignUpLabel.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(HomeActivity.this, "WAY TO SIGN UP MAN", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     findViewById(R.id.sign_in_button).setOnClickListener(this); 

     // Build GoogleApiClient with access to basic profile 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(Plus.API) 
       .addScope(new Scope(Scopes.PROFILE)) 
       .build(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     // onConnected indicates that an account was selected on the device, that the selected 
     // account has granted any requested permissions to our app and that we were able to 
     // establish a service connection to Google Play services. 
     Log.d(TAG, "onConnected:" + bundle); 
     mShouldResolve = false; 

     //Show the signed-in UI 
     showSignedInUI(); 
    } 

    @Override 
    public void onConnectionSuspended(int arg0) { 

     // what should i do here ? should i call mGoogleApiClient.connect() again ? ? 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_home, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     // Could not connect to Google Play Services. The user needs to select an account, 
     // grant permissions or resolve an error in order to sign in. Refer to the javadoc for 
     // ConnectionResult to see possible error codes. 

     Log.d(TAG, "onConnectionFailed:" + connectionResult); 

     if (!mIsResolving && mShouldResolve) { 
      if (connectionResult.hasResolution()) { 
       try { 
        connectionResult.startResolutionForResult(this, RC_SIGN_IN); 
        mIsResolving = true; 
       } catch (IntentSender.SendIntentException e) { 
        Log.e(TAG, "Could not resolve ConnectionResult.", e); 
        mIsResolving = false; 
        mGoogleApiClient.connect(); 
       } 
      } else { //    // Could not resolve the connection result, show the user an //    // error dialog. //    showErrorDialog(connectionResult); 
      } 
     } else { //   // Show the signed-out UI //   showSignedOutUI(); 
     } 
    } 

// private void showErrorDialog(ConnectionResult connectionResult) { // // } // // private void showSignedOutUI() { // // } 

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

    @Override 
    protected void onStop() { 
     super.onStop(); 
     mGoogleApiClient.disconnect(); 
    } 

    private void onSignInClicked() { 
     // User clicked the sing-in button, so begin the sign-in process and automatically 
     // attempt to resolve any errors that occur. 
     mShouldResolve = true; 
     mGoogleApiClient.connect(); 

     // Show a message to the user that we are signing in. 
     mStatusTextView.setText(R.string.signing_in); 
    } 

    @Override 
    public void onClick(View v) { 

     if (v.getId() == R.id.sign_in_button) { 
      onSignInClicked(); 
     } 

    } 

    private void showSignedInUI() { 

    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data); 

     if (requestCode == RC_SIGN_IN) { 
      // If the error resolution was not successful we should not resolve it further. 
      if (resultCode != RESULT_OK) { 
       mShouldResolve = false; 
      } 

      mIsResolving = false; 
      mGoogleApiClient.connect(); 
     } 
    } 

} 

回答

1

您還沒有使用findViewByID調用設置mStatusTextView,因此它被綁定爲NULL。 在這種方法中:

protected void onCreate(Bundle savedInstanceState) { 

你需要做的,你與其他小部件做了什麼:

mStatusTextView = (TextView) findViewById(R.id.TEXTVIEW_ID_HERE); 
+0

好是有道理的。後續問題...因爲它是谷歌的小部件,有沒有我應該導入的TextView?那就是TextView,我會指你說「TEXTVIEW_ID_HERE」的地方。再次,我對Android編碼非常陌生。 – zahudson95

+0

嘿謝謝。如果你查看XML文件,你需要添加一個元素,給它一個@id標籤,然後你可以用這個id標籤替換TEXTVIEW_ID_HERE! –