2017-04-05 51 views
-5

我潑活動代碼:我的應用程序中獲得墜毀

package com.example.hp_pc.earnmebucks; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 

public class SplashActivity extends AppCompatActivity { 
    Thread splashTread; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_splash); 
     Thread timerThread = new Thread() { 
      public void run() { 
       try { 
        sleep(3000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } finally { 
        Intent intent = new Intent(SplashActivity.this, Login.class); 
        startActivity(intent); 
       } 
      } 
     }; 
     timerThread.start(); 
    } 
} 

我Login.java代碼:

package com.example.hp_pc.earnmebucks; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.support.annotation.NonNull; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.auth.api.Auth; 
import com.google.android.gms.auth.api.signin.GoogleSignInAccount; 
import com.google.android.gms.auth.api.signin.GoogleSignInOptions; 
import com.google.android.gms.auth.api.signin.GoogleSignInResult; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.SignInButton; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.OptionalPendingResult; 
import com.google.android.gms.common.api.ResultCallback; 

public class Login extends AppCompatActivity implements 
     View.OnClickListener, 
     GoogleApiClient.OnConnectionFailedListener { 

    private static final String TAG = MainActivity.class.getSimpleName(); 
    private static final int RC_SIGN_IN = 007; 

    private GoogleApiClient mGoogleApiClient; 
    private ProgressDialog mProgressDialog; 

    private SignInButton btnSignIn; 
    private Button btnSignOut, btnRevokeAccess; 
    private LinearLayout llProfileLayout; 
    private ImageView imgProfilePic; 
    private TextView txtName, txtEmail; 


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

     super.getIntent(); 
     btnSignIn = (SignInButton) findViewById(R.id.sign_in_button); 

     btnSignIn.setOnClickListener(this); 



     GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       .requestEmail() 
       .build(); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .enableAutoManage(this, this) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
       .build(); 

     // Customizing G+ button 
     btnSignIn.setSize(SignInButton.SIZE_STANDARD); 
     btnSignIn.setScopes(gso.getScopeArray()); 
    } 


    private void signIn() { 
     Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
     startActivityForResult(signInIntent, RC_SIGN_IN); 
    } 



     private void handleSignInResult(GoogleSignInResult result) { 
      Log.d(TAG, "handleSignInResult:" + result.isSuccess()); 
      if (result.isSuccess()) { 
       // Signed in successfully, show authenticated UI. 
       GoogleSignInAccount acct = result.getSignInAccount(); 

       Log.e(TAG, "display name: " + acct.getDisplayName()); 



       updateUI(true); 
      } else { 
       // Signed out, show unauthenticated UI. 
       updateUI(false); 
      } 
     } 

     @Override 
     public void onClick(View v) { 
      int id = v.getId(); 

      switch (id) { 
       case R.id.sign_in_button: 
        signIn(); 
        break; 


      } 
     } 

     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 

      // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
      if (requestCode == RC_SIGN_IN) { 
       GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
       handleSignInResult(result); 
      } 
     } 

     @Override 
     public void onStart() { 
      super.onStart(); 

      OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient); 
      if (opr.isDone()) { 
       // If the user's cached credentials are valid, the OptionalPendingResult will be "done" 
       // and the GoogleSignInResult will be available instantly. 
       Log.d(TAG, "Got cached sign-in"); 
       GoogleSignInResult result = opr.get(); 
       handleSignInResult(result); 
      } else { 
       // If the user has not previously signed in on this device or the sign-in has expired, 
       // this asynchronous branch will attempt to sign in the user silently. Cross-device 
       // single sign-on will occur in this branch. 
       showProgressDialog(); 
       opr.setResultCallback(new ResultCallback<GoogleSignInResult>() { 
        @Override 
        public void onResult(GoogleSignInResult googleSignInResult) { 
         hideProgressDialog(); 
         handleSignInResult(googleSignInResult); 
        } 
       }); 
      } 
     } 

     @Override 
     public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
      // An unresolvable error has occurred and Google APIs (including Sign-In) will not 
      // be available. 
      Log.d(TAG, "onConnectionFailed:" + connectionResult); 
     } 

     private void showProgressDialog() { 
      if (mProgressDialog == null) { 
       mProgressDialog = new ProgressDialog(this); 
       mProgressDialog.setMessage("loading"); 
       mProgressDialog.setIndeterminate(true); 
      } 

      mProgressDialog.show(); 
     } 

     private void hideProgressDialog() { 
      if (mProgressDialog != null && mProgressDialog.isShowing()) { 
       mProgressDialog.hide(); 
      } 
     } 

     private void updateUI(boolean isSignedIn) { 
      if (isSignedIn) { 
       btnSignIn.setVisibility(View.GONE); 
       Intent i = new Intent(Login.this,Signin.class); 
       startActivity(i); 
      } else { 
       btnSignIn.setVisibility(View.VISIBLE); 
       btnSignOut.setVisibility(View.GONE); 
       btnRevokeAccess.setVisibility(View.GONE); 
       llProfileLayout.setVisibility(View.GONE); 
      } 
     } 
} 

而且它的XML代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="4"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2" 
     android:gravity="center_horizontal" 
     android:orientation="vertical"> 


    </LinearLayout> 


    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2"> 

     <com.google.android.gms.common.SignInButton 
      android:id="@+id/sign_in_button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:visibility="visible" 
      tools:visibility="gone" /> 




    </RelativeLayout> 

</LinearLayout> 

我logcat的是:

at com.google.android.gms.auth.api.Auth$4.zza(Unknown Source) 
    at com.google.android.gms.common.api.GoogleApiClient$Builder.zza(Unknown Source) 
    at com.google.android.gms.common.api.GoogleApiClient$Builder.zzuQ(Unknown Source) 
    at com.google.android.gms.common.api.GoogleApiClient$Builder.build(Unknown Source) 
    at com.example.hp_pc.earnmebucks.Login.onCreate(Login.java:67) 
    at android.app.Activity.performCreate(Activity.java:6001) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368) 
    at android.app.ActivityThread.access$800(ActivityThread.java:144) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5233) 
    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:898) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 

請幫忙。飛濺活動後,我的應用程序崩潰。如果有人能幫助我,我將非常感激。我不知道空指針異常在哪裏。 在此先感謝。

+0

post stacktrace – Krish

+0

請包括logcat輸出 – HomerPlata

+1

您沒有顯示異常本身。哪個例外? –

回答

1

問題是與

Thread timerThread = new Thread() { 
     public void run() { 
      try { 
       sleep(3000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } finally { 
       Intent intent = new Intent(SplashActivity.this, Login.class); 
       startActivity(intent); 
      } 
     } 
    }; 
    timerThread.start(); 

你不能剛開始從非UI線程意圖。 使用runOnUiThread它

所以,你的代碼看起來應該像

 Thread timerThread = new Thread() { 
      public void run() { 
       try { 
        sleep(3000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } finally { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Intent intent = new Intent(SplashActivity.this, Login.class); 
          SplashActivity.this.startActivity(intent); 
         } 
        }); 
       } 
      } 
     }; 
     timerThread.start(); 

你也可以把它simplier使用CountDownTimer

new CountDownTimer(3000, 1000) { 

    public void onTick(long millisUntilFinished) { 
    } 
    public void onFinish() { 
     Intent intent = new Intent(SplashActivity.this, Login.class); 
     startActivity(intent); 
    } 

}.start(); 
1

試試這個: -

new Handler().postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(getApplicationContext(), Login.class); 
      startActivity(i); 
      finish(); 
     } 
    }, 1000); 
0

編譯「 com.google.android.gms:play-services-auth:10.2.1'

//添加到文件 底部的應用插件:「com.google.gms.google服務」

後添加這個在你的build.gradle同步並運行。

相關問題