我将我的Android项目从API 21更新为API 22,但我根本无法执行任何点击。这是logcat给我的错误
05-27 12:09:41.881 29097-29485/com.app.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.app.app, PID: 29097
java.lang.IllegalStateException: Could not find a method onLogin(View) in the activity class android.support.v7.internal.view.ContextThemeWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'login_button'
at android.view.View$1.onClick(View.java:3828)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18482)
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:5097)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: onLogin [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getMethod(Class.java:857)
at android.view.View$1.onClick(View.java:3821)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18482)
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:5097)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
在更新到api v22
之前,我的按钮工作正常 <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"
tools:context="com.app.app.AccountActivity"
android:background="@color/main">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:weightSum="2"
android:id="@+id/linearLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account_register"
android:id="@+id/register_button"
android:layout_weight="1"
android:background="#FF4F8FFF"
android:textColor="#FFFFFFFF"
android:textSize="20sp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:onClick="onRegister"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account_login"
android:id="@+id/login_button"
android:layout_weight="1"
android:background="#FF2055A9"
android:textColor="#FFFFFFFF"
android:textSize="20sp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:onClick="onLogin"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect with Twitter"
android:id="@+id/twitter_button"
android:layout_above="@+id/linearLayout"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignEnd="@+id/linearLayout"
android:layout_alignRight="@+id/linearLayout"
android:background="#FF1C1347"
android:textColor="#FFFFFFFF"
android:textSize="20sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:visibility="gone"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account_facebook"
android:id="@+id/facebook_button"
android:layout_above="@+id/twitter_button"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignEnd="@+id/linearLayout"
android:layout_alignRight="@+id/linearLayout"
android:background="#FF32246E"
android:textColor="#FFFFFFFF"
android:textSize="20sp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:onClick="onFacebookLogin" />
<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.facebook.login.widget.LoginButton"
android:id="@+id/facebook_auth"
android:layout_above="@+id/facebook_button"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:visibility="gone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/support"
android:id="@+id/textView19"
android:layout_above="@+id/facebook_button"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/support_message"
android:id="@+id/textView20"
android:layout_above="@+id/textView19"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView19"
android:background="#FF32246E"
android:id="@+id/login_in_view"
android:visibility="gone"
android:gravity="center">
<view
android:layout_width="30dp"
android:layout_height="30dp"
class="com.github.rahatarmanahmed.cpv.CircularProgressView"
android:id="@+id/view3"
app:cpv_indeterminate="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/textView20">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView4"
android:src="@drawable/landing_2x"
android:scaleType="centerInside"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:src="@drawable/logo_landing"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</RelativeLayout>
关于我的活动
package com.app.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.app.app.support.AppJsonRequest;
import com.app.app.support.AppNetwork;
import com.app.app.support.AppSession;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Arrays;
public class AccountActivity extends ActionBarActivity {
public static final int REGISTER_REQUEST_CODE = 1;
public static final int LOGIN_REQUEST_CODE = 2;
private LoginButton fb_login = null;
private Button login;
private Button register;
private CallbackManager manager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
login = (Button) findViewById(R.id.login_button);
register = (Button) findViewById(R.id.register_button);
fb_login = (LoginButton) findViewById(R.id.facebook_auth);
fb_login.setReadPermissions(Arrays.asList("user_hometown", "user_location", "user_status", "public_profile", "user_photos", "email", "user_friends"));
manager = CallbackManager.Factory.create();
fb_login.registerCallback(manager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.i("App", "Login FB success");
onFacebookLogin();
}
@Override
public void onCancel() {
String msg = getString(R.string.fb_login_cancelled);
Toast toast = Toast.makeText(AccountActivity.this, msg, Toast.LENGTH_SHORT);
toast.show();
enableButtons();
}
@Override
public void onError(FacebookException e) {
Toast toast = Toast.makeText(AccountActivity.this, e.getMessage(), Toast.LENGTH_SHORT);
toast.show();
enableButtons();
}
});
}
@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, menu);
return false;
}
@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
protected void onStart() {
super.onStart();
Tracker tracker = ((AppApp) getApplication()).getTracker();
tracker.setScreenName("Account");
tracker.send(new HitBuilders.AppViewBuilder().build());
}
public void disableButtons() {
login.setEnabled(false);
register.setEnabled(false);
//facebook.setEnabled(false);
findViewById(R.id.login_in_view).setVisibility(View.VISIBLE);
}
public void enableButtons() {
login.setEnabled(true);
register.setEnabled(true);
//facebook.setEnabled(true);
findViewById(R.id.login_in_view).setVisibility(View.GONE);
}
public void onFacebookLogin(View view) {
if (AppApp.DEBUG) Log.i("App", "Facebook login");
disableButtons();
fb_login.performClick();
}
private void onFacebookLogin() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken != null) {
GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
onFacebookUser(object);
}
}).executeAsync();
}
}
private void onFacebookUser(JSONObject user) {
AppSession.onFacebookLogin(this, user.optString("id", "0"), user.optString("email", "username@domain.com"));
AppJsonRequest request = AppNetwork.newAppRequest("register.php");
request.addParam("name", user.optString("first_name", "Name"));
request.addParam("last", user.optString("last_name", "Last"));
request.addParam("email", user.optString("email", "username@domain.com"));
request.addParam("sex", user.optString("gender", "undefined"));
request.addParam("fb", user.optString("id", "0"));
request.setListener(new AppJsonRequest.Listener() {
@Override
public void onResult(JSONObject result) {
int id = result.optInt("user", 0);
if (id != 0) {
AppSession.onLogin(AccountActivity.this, id);
// we will call this from main
//updateFacebook();
getFacebookFriends();
goToMain();
} else if (AppApp.DEBUG) {
Log.e("App", "register.php error " + result.toString());
}
}
@Override
public void onError(AppJsonRequest.ERROR error) {
if (AppApp.DEBUG) Log.e("App", "AppJsonRequest error " + error);
}
});
request.execute();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
manager.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REGISTER_REQUEST_CODE:
case LOGIN_REQUEST_CODE:
if (resultCode == RESULT_OK) {
goToMain();
}
break;
default:
break;
}
}
private void goToMain() {
Intent intent = new Intent(this, OnBoardingActivity.class);
startActivity(intent);
finish();
}
public void updateFacebook() {
AccessToken token = AccessToken.getCurrentAccessToken();
GraphRequest request = GraphRequest.newGraphPathRequest(token, "/me/home", new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
JSONObject response = graphResponse.getJSONObject();
JSONArray array = response.optJSONArray("data");
if (array != null) {
AppJsonRequest request = AppNetwork.newAppRequest("checkin.php");
request.addParam("data", array.toString());
request.addParam("from", "facebook");
request.addParam("user", AppSession.USER);
request.addParam("access_token", AccessToken.getCurrentAccessToken().getToken());
request.execute();
}
}
});
Bundle bundle = new Bundle();
bundle.putString("fields", "from,place,message,object_id,type");
bundle.putString("limit", "100");
bundle.putString("with", "location");
request.setParameters(bundle);
request.executeAsync();
}
public void onRegister(View view) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivityForResult(intent, REGISTER_REQUEST_CODE);
}
public void onLogin(View view) {
Intent intent = new Intent(this, LoginActivity.class);
startActivityForResult(intent, LOGIN_REQUEST_CODE);
}
public void getFacebookFriends() {
AccessToken token = AccessToken.getCurrentAccessToken();
GraphRequest request = GraphRequest.newMyFriendsRequest(token, new GraphRequest.GraphJSONArrayCallback() {
@Override
public void onCompleted(JSONArray jsonArray, GraphResponse graphResponse) {
AppJsonRequest request = AppNetwork.newAppRequest("friendsLocation.php");
request.addParam("from", "facebook");
request.addParam("data", jsonArray.toString());
request.addParam("user", AppSession.USER);
request.execute();
}
});
Bundle bundle = new Bundle();
bundle.putString("fields", "id,name,location,hometown");
request.setParameters(bundle);
request.executeAsync();
}
}