我目前遇到问题,我认为可能是我的按钮。 来自Android Studio:我已经创建了一个应用程序,目前,从一个页面(AccountListActivity)单击一个按钮应该重定向到另一个页面(SelectSNActivity)。它之前运作良好,但在我最近添加了Google+登录选项后(在SelectSNActivity上),该按钮将无效,而是将我重定向回起始页。
我想知道代码的问题是否可能在AccountListActivity中(因为那是按钮从中发起Intent的地方。
我没有错误,能够在模拟器和实际设备上运行应用程序。如果您能够发现错误的来源,或者您希望我通过其他文件,请不要犹豫,在下面发表评论或发帖,非常感谢。
这是:
。AccountListActivity的.java代码
package com.instigate.aggregator06;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
public class AccountListActivity extends AppCompatActivity {
public HashMap accountList = new HashMap();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account_list);
TextView emptyview = (TextView) findViewById(R.id.empty);
ListView accountslist=(ListView) findViewById(R.id.list);
emptyview.setVisibility(View.VISIBLE);
accountslist.setVisibility(View.GONE);
/*if (accountList.length() == 0)
{
emptyview.setVisibility(View.GONE);
accountslist.setVisibility(View.VISIBLE);
}
else
{
accountslist.setVisibility(View.VISIBLE);
emptyview.setVisibility(View.GONE);
} */
}
@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_account_list, 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);
}
public void goToSelectSNPage(View view)
{
Intent k = new Intent(AccountListActivity.this,SelectSNActivity.class);
startActivity(k);
}
}
xml代码AccountListActivity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:id="@+id/accountPage">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/accountPageHeader"
android:id="@+id/accountPageHeader"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="36sp"/>
<LinearLayout
android:layout_below="@id/accountPageHeader"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/noAccounts"
android:visibility="visible"
android:textSize="24sp"
/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:visibility="gone">
<ListView android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</ScrollView>
<Button android:id="@+id/addAccount"
android:text="@string/addAccount"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="goToSelectSNPage"/>
</LinearLayout>
</RelativeLayout>
。SelectSNActivity的.java代码
package com.instigate.aggregator06;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.plus.Plus;
/**
* Minimal activity demonstrating basic Google Sign-In.
*/
public class SelectSNActivity extends AppCompatActivity implements
View.OnClickListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "SelectSNActivity";
/* RequestCode for resolutions involving sign-in */
private static final int RC_SIGN_IN = 9001;
/* Keys for persisting instance variables in savedInstanceState */
private static final String KEY_IS_RESOLVING = "is_resolving";
private static final String KEY_SHOULD_RESOLVE = "should_resolve";
/* Client for accessing Google APIs */
private GoogleApiClient mGoogleApiClient;
/* View to display current status (signed-in, signed-out, disconnected, etc) */
private TextView mStatus;
/* Is there a ConnectionResult resolution in progress? */
private boolean mIsResolving = false;
/* Should we automatically resolve ConnectionResults when possible? */
private boolean mShouldResolve = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Restore from saved instance state
// [START restore_saved_instance_state]
if (savedInstanceState != null) {
mIsResolving = savedInstanceState.getBoolean(KEY_IS_RESOLVING);
mShouldResolve = savedInstanceState.getBoolean(KEY_SHOULD_RESOLVE);
}
// [END restore_saved_instance_state]
// Set up button click listeners
findViewById(R.id.googleSignInBtn).setOnClickListener(this);
//findViewById(R.id.sign_out_button).setOnClickListener(this);
//findViewById(R.id.disconnect_button).setOnClickListener(this);
// Large sign-in
((SignInButton) findViewById(R.id.googleSignInBtn)).setSize(SignInButton.SIZE_WIDE);
// Start with sign-in button disabled until sign-in either succeeds or fails
findViewById(R.id.googleSignInBtn).setEnabled(false);
// Set up view instances
//mStatus = (TextView) findViewById(R.id.status);
// [START create_google_api_client]
// 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();
// [END create_google_api_client]
}
private void updateUI(boolean isSignedIn) {
if (isSignedIn) {
// Show signed-in user's name
String name = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getDisplayName();
mStatus.setText(getString(R.string.signed_in_fmt, name));
// Set button visibility
findViewById(R.id.googleSignInBtn).setVisibility(View.GONE);
//findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
} else {
// Show signed-out message
mStatus.setText(R.string.signed_out);
// Set button visibility
findViewById(R.id.googleSignInBtn).setEnabled(true);
findViewById(R.id.googleSignInBtn).setVisibility(View.VISIBLE);
//findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);
}
}
// [START on_start_on_stop]
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
// [END on_start_on_stop]
// [START on_save_instance_state]
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(KEY_IS_RESOLVING, mIsResolving);
outState.putBoolean(KEY_SHOULD_RESOLVE, mIsResolving);
}
// [END on_save_instance_state]
// [START on_activity_result]
@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 further errors.
if (resultCode != RESULT_OK) {
mShouldResolve = false;
}
mIsResolving = false;
mGoogleApiClient.connect();
}
}
// [END on_activity_result]
@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);
// Show the signed-in UI
updateUI(true);
}
@Override
public void onConnectionSuspended(int i) {
// The connection to Google Play services was lost. The GoogleApiClient will automatically
// attempt to re-connect. Any UI elements that depend on connection to Google APIs should
// be hidden or disabled until onConnected is called again.
Log.w(TAG, "onConnectionSuspended:" + i);
}
// [START on_connection_failed]
@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
updateUI(false);
}
}
// [END on_connection_failed]
private void showErrorDialog(ConnectionResult connectionResult) {
int errorCode = connectionResult.getErrorCode();
if (GooglePlayServicesUtil.isUserRecoverableError(errorCode)) {
// Show the default Google Play services error dialog which may still start an intent
// on our behalf if the user can resolve the issue.
GooglePlayServicesUtil.getErrorDialog(errorCode, this, RC_SIGN_IN,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mShouldResolve = false;
updateUI(false);
}
}).show();
} else {
// No default Google Play Services error, display a message to the user.
String errorString = getString(R.string.play_services_error_fmt, errorCode);
Toast.makeText(this, errorString, Toast.LENGTH_SHORT).show();
mShouldResolve = false;
updateUI(false);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.googleSignInBtn:
// User clicked the sign-in button, so begin the sign-in process and automatically
// attempt to resolve any errors that occur.
mStatus.setText(R.string.signing_in);
// [START sign_in_clicked]
mShouldResolve = true;
mGoogleApiClient.connect();
// [END sign_in_clicked]
break;
/*case R.id.sign_out_button:
// Clear the default account so that GoogleApiClient will not automatically
// connect in the future.
// [START sign_out_clicked]
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
// [END sign_out_clicked]
updateUI(false);
break;
case R.id.disconnect_button:
// Revoke all granted permissions and clear the default account. The user will have
// to pass the consent screen to sign in again.
// [START disconnect_clicked]
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
// [END disconnect_clicked]
updateUI(false);
break;*/
}
}
public void goToSignInPage(View view)
{
Intent j = new Intent(SelectSNActivity.this, SignInActivity.class);
startActivity(j);
}
}
SelectSNActivity的xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.instigate.aggregator06.SelectSNActivity"
android:orientation="vertical"
android:id="@+id/SelectSNActivity"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/selectSocialNetwork"
android:id="@+id/selectSocialNetworkHeader"
android:textStyle="bold"
android:textSize="36sp"
android:layout_gravity="center_horizontal"
/>
<com.google.android.gms.common.SignInButton
android:id="@+id/googleSignInBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fbBtn"
android:id="@+id/fbBtn"
android:onClick="goToSignInPage"/>
</LinearLayout>
请注意,在使用Heisenberg的修复程序后,我运行了该应用程序。它打开SelectSNActivity,但它会快速退出并返回MainActivity页面。以下是我理解的两个错误:
com.instigate.aggregator06.SelectSNActivity.updateUI(SelectSNActivity.java:83)
指的是
// Show signed-out message
mStatus.setText(R.string.signed_out);
和
com.instigate.aggregator06.SelectSNActivity.onConnectionFailed(SelectSNActivity.java:165)
指的是
/ Could not resolve the connection result, show the user an
// error dialog.
showErrorDialog(connectionResult);
}
} else {
//Show the signed-out UI
updateUI(false); <--------- this line
}
}
这是整个错误日志
07-21 16:47:46.572 138-507/? E/ALSAModule﹕ buffersize: 6144, periodsize:2048
07-21 16:47:46.582 138-507/? E/ALSAModule﹕ audio type flag: 0
07-21 16:47:46.582 138-507/? E/ALSAModule﹕ buffersize: 6144, periodsize:2048
07-21 16:47:46.582 138-507/? E/ALSAModule﹕ audio type flag: 0
07-21 16:48:00.012 515-529/system_process E/WindowManager﹕ Starting window AppWindowToken{4202bd70 token=Token{41cfb398 ActivityRecord{41bf3338 u0 com.ttxapps.wifiadb/.MainActivity t40}}} timed out
07-21 16:48:06.972 132-444/? E/FrameworkListener﹕ read() failed (Connection reset by peer)
07-21 16:48:21.062 23495-23495/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
07-21 16:48:21.062 23495-23495/? E/android.os.Debug﹕ failed to load memtrack module: -2
07-21 16:48:33.862 860-860/com.google.process.gapps E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
07-21 16:48:33.862 860-860/com.google.process.gapps E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
07-21 16:48:33.982 860-860/com.google.process.gapps E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
07-21 16:48:33.982 860-860/com.google.process.gapps E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
07-21 16:48:34.852 23641-23641/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
07-21 16:48:34.852 23641-23641/? E/android.os.Debug﹕ failed to load memtrack module: -2
07-21 16:48:35.522 23177-23177/com.ttxapps.wifiadb E/WifiADB﹕ IabPurchase: failed to query inventory: IabResult: Error refreshing inventory (querying prices of items). (response: 6:Error)
07-21 16:48:39.632 138-507/? E/ALSAModule﹕ buffersize: 6144, periodsize:2048
07-21 16:48:39.632 138-507/? E/ALSAModule﹕ audio type flag: 0
07-21 16:48:39.642 138-507/? E/ALSAModule﹕ buffersize: 6144, periodsize:2048
07-21 16:48:39.642 138-507/? E/ALSAModule﹕ audio type flag: 0
07-21 16:48:41.302 23657-23657/com.instigate.aggregator06 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.instigate.aggregator06, PID: 23657
java.lang.NullPointerException
at com.instigate.aggregator06.SelectSNActivity.updateUI(SelectSNActivity.java:83)
at com.instigate.aggregator06.SelectSNActivity.onConnectionFailed(SelectSNActivity.java:165)
at com.google.android.gms.common.internal.zzj.zzh(Unknown Source)
at com.google.android.gms.common.api.zze.zzd(Unknown Source)
at com.google.android.gms.common.api.zze.zza(Unknown Source)
at com.google.android.gms.common.api.zze.zza(Unknown Source)
at com.google.android.gms.common.api.zze$zzb$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你是geiven setContentView(R.layout.activity_main);在SelectSNActivity的oncreate()中。用setContentView(R.layout.activity_select_sn)替换它;