我正在实施Facebook登录我的Android应用程序..
但是当我运行应用程序时,我得到了问题..
这里是 Logcat ..
protoc
这是我的活动
LoginActivity.java
Sensor.java
我在这部分遇到问题..
12-07 16:04:51.687 3556-3556/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.administrator.mosbeau, PID: 3556
java.lang.ExceptionInInitializerError
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:436)
at android.app.Activity.setContentView(Activity.java:2245)
at com.example.administrator.mosbeau.LoginActivity.onCreate(LoginActivity.java:60)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: null
at com.facebook.internal.Validate.sdkInitialized(Validate.java:99)
at com.facebook.FacebookSdk.getCallbackRequestCodeOffset(FacebookSdk.java:735)
at com.facebook.internal.CallbackManagerImpl$RequestCodeOffset.toRequestCode(CallbackManagerImpl.java:109)
at com.facebook.login.widget.LoginButton.<clinit>(LoginButton.java:58)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:436)
at android.app.Activity.setContentView(Activity.java:2245)
at com.example.administrator.mosbeau.LoginActivity.onCreate(LoginActivity.java:60)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
我添加此代码
package com.example.administrator.mosbeau;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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 org.json.JSONObject;
@SuppressWarnings("deprecation")
public class LoginActivity extends Activity implements View.OnClickListener {
Button btnSignin;
EditText txtEmail, txtPassword;
TextView txtSignup, txtForgot;
UserLocalStore userLocalStore;
private CallbackManager callbackManager;
private LoginButton loginButton;
private Button btnLoginfb;
private ProgressDialog progressDialog;
FbUser FbUser;
String Expn =
"^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
+"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
//boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
if(isConnected){
}else{
nointernet();
}
txtEmail = (EditText) findViewById(R.id.txtEmail);
txtPassword = (EditText) findViewById(R.id.txtPassword);
btnSignin = (Button) findViewById(R.id.btnSubmit);
btnSignin.setOnClickListener(this);
txtSignup = (TextView) findViewById(R.id.textSignup);
txtSignup.setOnClickListener(this);
txtForgot = (TextView) findViewById(R.id.textForgot);
txtForgot.setOnClickListener(this);
userLocalStore = new UserLocalStore(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnSubmit:
String customers_email_address = txtEmail.getText().toString();
String customers_password = txtPassword.getText().toString();
if(customers_email_address.length()==0){
txtEmail.setError("Email Address is required.");
}
else if(customers_email_address.length()<=4){
txtEmail.setError("Email Address must be at least 5 characters long.");
}
else if(customers_email_address.matches(Expn)){
if(customers_password.length()==0){
txtPassword.setError("Password is required.");
}
else if(customers_password.length()<=6){
txtPassword.setError("Your Password must contain a minimum of 7 characters!");
}
else {
User user = new User(customers_email_address, customers_password);
authenticate(user);
}
}else{
txtEmail.setError("Email Address is not valid.");
}
break;
case R.id.textSignup:
Intent myIntent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(myIntent);
break;
case R.id.textForgot:
Intent myForgotIntent = new Intent(LoginActivity.this, ForgotPasswordActivity.class);
startActivity(myForgotIntent);
break;
}
}
private void authenticate(User user){
ServerRequests serverRequests = new ServerRequests(this);
serverRequests.fetchUSerDataInBackground(user, new GetUSerCallBack() {
@Override
public void done(User returnedUser) {
if (returnedUser == null) {
showErrorMessage();
} else {
logUserIn(returnedUser);
}
}
});
}
private void showErrorMessage(){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
//dialogBuilder.setTitle("Oops!");
dialogBuilder.setMessage("Please check your email or password.");
dialogBuilder.setPositiveButton("OK", null);
AlertDialog dialog = dialogBuilder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.show();
}
private void logUserIn(User returnedUser) {
userLocalStore.storeUserData(returnedUser);
userLocalStore.setUserLoggedIn(true);
Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(myIntent);
}
@Override
protected void onResume() {
super.onResume();
callbackManager=CallbackManager.Factory.create();
loginButton= (LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile", "email", "user_friends");
btnLoginfb= (Button) findViewById(R.id.btnLoginfb);
btnLoginfb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
loginButton.performClick();
loginButton.setPressed(true);
loginButton.invalidate();
loginButton.registerCallback(callbackManager, mCallBack);
loginButton.setPressed(false);
loginButton.invalidate();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
progressDialog.dismiss();
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.e("response: ", response + "");
try {
FbUser = new FbUser();
FbUser.facebookID = object.getString("id").toString();
FbUser.email = object.getString("email").toString();
FbUser.name = object.getString("name").toString();
FbUser.gender = object.getString("gender").toString();
PrefUtils.setCurrentUser(FbUser,LoginActivity.this);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(LoginActivity.this,"welcome "+FbUser.name,Toast.LENGTH_LONG).show();
//Intent intent=new Intent(LoginActivity.this,LogoutActivity.class);
//startActivity(intent);
//finish();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
progressDialog.dismiss();
}
@Override
public void onError(FacebookException e) {
progressDialog.dismiss();
}
};
public void nointernet(){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setMessage("There seems to be a problem with your connection.");
dialogBuilder.setNegativeButton("Edit Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Stop the activity
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
dialogBuilder.setPositiveButton("Reload", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Stop the activity
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
AlertDialog dialog = dialogBuilder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
}
}
这里
setContentView(R.layout.activity_login);
我现在收到此错误
FacebookSdk.sdkInitialize(getApplicationContext());
这是我的activity_login
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FacebookSdk.sdkInitialize(getApplicationContext());
当我点击登录按钮时,我有主要活动,它将进入loginactivity ..
这里是主要活动代码..
12-07 16:22:09.077 5663-5663/? E/ActivityThread﹕ Service com.facebook.mqttlite.MqttService has leaked IntentReceiver com.facebook.rti.mqtt.e.h@4910f74 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Service com.facebook.mqttlite.MqttService has leaked IntentReceiver com.facebook.rti.mqtt.e.h@4910f74 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:968)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:769)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1827)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1807)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:503)
at com.facebook.rti.mqtt.e.f.a(KeepaliveManager.java:170)
at com.facebook.rti.mqtt.f.i.<init>(MqttConnectionManager.java:193)
at com.facebook.rti.mqtt.f.an.a(MqttPushServiceBootstrap.java:473)
at com.facebook.rti.push.service.r.a(FbnsServiceBootstrap.java:64)
at com.facebook.mqttlite.MqttService.f(MqttService.java:473)
at com.facebook.rti.mqtt.f.af.c(MqttPushService.java:211)
at com.facebook.rti.mqtt.f.g.a(MqttBackgroundService.java:145)
at com.facebook.rti.mqtt.f.h.handleMessage(MqttBackgroundService.java:46)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
12-07 16:22:09.087 5663-5663/? E/ActivityThread﹕ Service com.facebook.mqttlite.MqttService has leaked IntentReceiver com.facebook.rti.mqtt.common.b.e@2d334e2d that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Service com.facebook.mqttlite.MqttService has leaked IntentReceiver com.facebook.rti.mqtt.common.b.e@2d334e2d that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:968)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:769)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1827)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1807)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1801)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:496)
at com.facebook.rti.mqtt.common.b.d.j(MqttNetworkManager.java:67)
at com.facebook.rti.mqtt.common.b.d.<init>(MqttNetworkManager.java:54)
at com.facebook.rti.mqtt.f.an.a(MqttPushServiceBootstrap.java:174)
at com.facebook.rti.push.service.r.a(FbnsServiceBootstrap.java:64)
at com.facebook.mqttlite.MqttService.f(MqttService.java:473)
at com.facebook.rti.mqtt.f.af.c(MqttPushService.java:211)
at com.facebook.rti.mqtt.f.g.a(MqttBackgroundService.java:145)
at com.facebook.rti.mqtt.f.h.handleMessage(MqttBackgroundService.java:46)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
12-07 16:22:09.087 5663-5886/? W/msgr(:<default>):QuickExperimentControllerImpl﹕ Exposure of experiment com.facebook.widget.tiles.annotations.c@2487e599 occurred when no user was logged in
12-07 16:22:09.087 5663-5663/? E/ActivityThread﹕ Service com.facebook.mqttlite.MqttService has leaked IntentReceiver com.facebook.rti.mqtt.common.d.t@315780dc that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Service com.facebook.mqttlite.MqttService has leaked IntentReceiver com.facebook.rti.mqtt.common.d.t@315780dc that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:968)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:769)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1827)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1807)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:503)
at com.facebook.rti.mqtt.common.d.s.<init>(WakingExecutorService.java:90)
at com.facebook.rti.mqtt.f.an.a(MqttPushServiceBootstrap.java:304)
at com.facebook.rti.push.service.r.a(FbnsServiceBootstrap.java:64)
at com.facebook.mqttlite.MqttService.f(MqttService.java:473)
at com.facebook.rti.mqtt.f.af.c(MqttPushService.java:211)
at com.facebook.rti.mqtt.f.g.a(MqttBackgroundService.java:145)
at com.facebook.rti.mqtt.f.h.handleMessage(MqttBackgroundService.java:46)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
请帮帮我..
感谢
乔
答案 0 :(得分:1)
在oncreate
之后setContentView
初始化SDK,如下所示,并从onResume
/*Init Facebook SDK */
FacebookSdk.sdkInitialize(this.getApplicationContext());
您可以在初始化SDK之后尝试使用以下方法进行日志记录而不使用Facebook登录按钮,不需要在另一个按钮单击中调用它performClick()
。
// Facebook user data access permissions
private final String PERMISSIONS[] = new String[]{"public_profile", "user_birthday", "email"};
/**
* Login to app with Facebook account
*/
private void doFacebookLogin() {
//Initialize mCallbackManager
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList(PERMISSIONS));
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// Login success, now request for user details
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
if (response.getError() != null) {
// failed to fetch user details
} else {
// Successfully retrieved user details
String email, firstName, gender, birthday;
try {
if (object.has("email")) {
email = object.getString("email");
}
if (object.has("first_name")) {
firstName = object.getString("first_name");
}
if (object.has("gender")) {
gender = object.getString("gender");
}
if (object.has("birthday")) {
birthday = object.getString("birthday");
}
} catch (JSONException e) {
// failed to parse facebook response
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "email,first_name,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
不要忘记在清单中保留下面的片段
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
除非您需要,否则从XML中删除com.facebook.login.widget.LoginButton
。
答案 1 :(得分:0)
将此行添加到OnCreateView
LoginActivity
FacebookSdk.sdkInitialize(getApplicationContext());
让我知道它是否正常工作