//登录时登录Google App崩溃时出现问题,我只是想从登录中获取数据并发送到另一个活动即Pic.java,但它崩溃请让我知道在哪里place给代码来修复这段代码,我已经把代码放在gsignIn()下了。
package app.pack.name;
public class Login extends AppCompatActivity implements OnConnectionFailedListener, View.OnClickListener, ConnectionCallbacks {
GoogleApiClient mGoogleApiClient;
GoogleSignInOptions gso;
SignInButton signIn_btn;
SharedPreferences sharedPreferences;
boolean loggedIn;
private static final int RC_SIGN_IN = 0;
String name,email;
ProgressDialog progress_dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
sharedPreferences = getSharedPreferences("APP", MODE_PRIVATE);
name = sharedPreferences.getString("name", "");
email = sharedPreferences.getString("e_mail", "");
String ID = sharedPreferences.getString("ID", "");
loggedIn = sharedPreferences.getBoolean("isLogin", false);
buidNewGoogleApiClient();
customizeSignBtn();
setBtnClickListeners();
progress_dialog = new ProgressDialog(this);
progress_dialog.setMessage("Signing in....");
}
/*
Configure sign-in to request the user's ID, email address, and basic profile.
User's ID and basic profile are included in DEFAULT_SIGN_IN.
create and initialize GoogleApiClient object to use Google Sign-In API and the options specified by gso..
*/
private void buidNewGoogleApiClient(){
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();
}
/*
Customize sign-in button. The sign-in button can be displayed in
multiple sizes and color schemes. It can also be contextually
rendered based on the requested scopes. For example. a red button may
be displayed when Google+ scopes are requested, but a white button
may be displayed when only basic profile is requested. Try adding the
Plus.SCOPE_PLUS_LOGIN scope to see the difference.
*/
private void customizeSignBtn(){
signIn_btn = (SignInButton) findViewById(R.id.sign_in_button);
signIn_btn.setSize(SignInButton.SIZE_STANDARD);
signIn_btn.setScopes(gso.getScopeArray());
}
/*
Set on click Listeners on the sign-in sign-out and disconnect buttons
*/
private void setBtnClickListeners(){
// Button listeners
signIn_btn.setOnClickListener(this);
findViewById(R.id.sign_out_button).setOnClickListener(this);
findViewById(R.id.disconnect_button).setOnClickListener(this);
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@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_main, 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 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) {
if (resultCode != RESULT_OK) {
progress_dialog.dismiss();
}
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
getSignInResult(result);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
Toast.makeText(this, "start sign process", Toast.LENGTH_SHORT).show();
gSignIn();
break;
case R.id.sign_out_button:
Toast.makeText(this, "Google Sign Out", Toast.LENGTH_SHORT).show();
gSignOut();
break;
case R.id.disconnect_button:
Toast.makeText(this, "Google Access Revoked", Toast.LENGTH_SHORT).show();
gRevokeAccess();
break;
case R.id.day_id:
Intent intent = new Intent(Login.this,Camera.class);
startActivity(intent);
break;
case R.id.day_ids:
Intent intents = new Intent(Login.this,Camera.class);
startActivity(intents);
break;
}
}
private void gSignIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
progress_dialog.show();
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// String mPhoneNumber = tMgr.getLine1Number();
String combine = name + "~" + tMgr.getLine1Number() + "~" + email;
Intent i = new Intent(this, Pic.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("combine_data", combine);
// bundle.putString("combine_data2",email);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
finish();
}
private void gSignOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
updateUI(false);
}
});
}
private void gRevokeAccess() {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
updateUI(false);
}
});
}
private void getSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
String id_token = acct.getIdToken(); //add this code here to save it by use SharedPreferences
TextView user_name= (TextView)findViewById(R.id.userName);
TextView email_id= (TextView)findViewById(R.id.emailId);
user_name.setText("UserName: "+ acct.getDisplayName());
email_id.setText("Email Id: " + acct.getEmail());
updateUI(true);
SharedPreferences sharedPreferences = getSharedPreferences("APP", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isLogin", true);
editor.putString("name", acct.getDisplayName());
editor.putString("e_mail", acct.getEmail());
editor.putString("ID", acct.getIdToken());
editor.commit();
progress_dialog.dismiss();
} else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
private void updateUI(boolean signedIn) {
if (signedIn) {
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);
}
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
// xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/sign_out_and_disconnect"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" user name:"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:textColor="@android:color/black"
android:textSize="14sp"
/>
<TextView
android:id="@+id/emailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="email id:"
android:textColor="@android:color/black"
android:layout_below="@+id/userName"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:textSize="14sp"
/>
<Button
android:id="@+id/sign_out_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Google Sign out"/>
<Button
android:id="@+id/disconnect_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="revoke Google Access"/>
</LinearLayout>
<LinearLayout
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_marginTop="290dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/day_id"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/day_form"/>
<ImageView
android:id="@+id/day_ids"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="90dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:src="@drawable/day_form"/>
</LinearLayout>
</RelativeLayout>
// logcat的
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: FATAL EXCEPTION: main
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: Process: app.pack.app.pack.name, PID: 7755
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{app.pack.app.pack.name/app.pack.app.pack.name.Pic}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5021)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at app.pack.app.pack.name.Pic.onCreate(Pic.java:33)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5231)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5021)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
12-22 17:39:45.160 7755-7755/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
12-22 17:39:45.160 26263-26442/? I/ActivityManager: Process app.pack.app.pack.name (pid 7755) has died.
12-22 17:39:45.160 26263-26442/? W/ActivityManager: Force removing ActivityRecord{2e93d7b4 u0 app.pack.app.pack.name/.Pic t38}: app died, no saved state
12-22 17:39:45.160 26263-26442/? D/ActivityManager: TopActivityInfo, pkgName: app.pack.app.pack.name activityName: app.pack.app.pack.name/com.google.android.gms.auth.api.signin.internal.SignInHubActivity callingPackage: bstSpecialAppKeyboardHandlingEnabled = false
12-22 17:39:45.160 26263-26442/? D/ActivityManager: Showing guidance for pkgName: app.pack.app.pack.name
12-22 17:39:45.190 26464-26464/? D/GuidanceScreen: event === app_launch
12-22 17:39:45.190 26464-26464/? D/GuidanceScreen: hiding guidance
12-22 17:39:45.190 26464-26464/? D/GuidanceScreen: hardKeyboard = 1
// Pic Activity
public class Pic extends Activity {
GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pic);
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String stuff = bundle.getString("combine_data");
// String sss = bundle.getString("combine_data2");
String max[] = stuff.split("~");
for (int i = 0; i < max[i].length(); i++)
System.out.println("binku " + max[i]);
}
}
// gSignIn()中的代码崩溃了应用程序,但我想将数据传递给另一个活动,所以我应该将此代码放在此活动中,以便应用程序不会崩溃
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// String mPhoneNumber = tMgr.getLine1Number();
String combine = name + "~" + tMgr.getLine1Number() + "~" + email;
Intent i = new Intent(this, Pic.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("combine_data", combine);
// bundle.putString("combine_data2",email);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
finish();
答案 0 :(得分:1)
String stuff = bundle.getString("combine_data");
如果此String为空或为空怎么办?你必须在这个操作之前检查一下。但是现在你的错误在这里:
String max[] = stuff.split("~");
for (int i = 0; i < max[i].length(); i++)
System.out.println("binku " + max[i]);
你的意思是“我&lt; max.length”?