Java Android错误:无法恢复活动。。?
我是android新手,尝试添加Facebook登录并在使用android studio使用Facebook sdk在android中单击注销按钮(使用AccessTokenTracker)后捕获事件,但我收到了此错误Java Android错误:无法恢复活动。。?,java,android,facebook-sdk-4.0,Java,Android,Facebook Sdk 4.0,我是android新手,尝试添加Facebook登录并在使用android studio使用Facebook sdk在android中单击注销按钮(使用AccessTokenTracker)后捕获事件,但我收到了此错误 这是日志, // (please horizontal scroll for logcat) 01-12 00:47:15.306 12572-12572/com.example.arpit.facebooklogindemo E/AndroidRuntime: FATAL EX
这是日志,
// (please horizontal scroll for logcat)
01-12 00:47:15.306 12572-12572/com.example.arpit.facebooklogindemo E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {com.example.arpit.facebooklogindemo/com.example.arpit.facebooklogindemo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.arpit.facebooklogindemo.MainActivity.onResume(MainActivity.java:83)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
at android.app.Activity.performResume(Activity.java:5211)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这是AndroidManifest.xml,<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arpit.facebooklogindemo">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
//from http://developers.facebook.com
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//from http://developers.facebook.com
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.arpit.facebooklogindemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView2" />
//from http://developers.facebook.com
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_above="@+id/login_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp" />
</RelativeLayout>
//从http://developers.facebook.com
//从http://developers.facebook.com
这里是activity_main.xml,<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arpit.facebooklogindemo">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
//from http://developers.facebook.com
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//from http://developers.facebook.com
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.arpit.facebooklogindemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView2" />
//from http://developers.facebook.com
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_above="@+id/login_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp" />
</RelativeLayout>
//从http://developers.facebook.com
这里是MainActivity.java,package com.example.arpit.facebooklogindemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class MainActivity extends AppCompatActivity {
CallbackManager callbackManager;
LoginButton loginButton;
TextView textView;
AccessTokenTracker accessTokenTracker;
private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
if(profile != null){
fillTextView(profile);
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
Log.d("find", String.valueOf(error));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
if(currentAccessToken == null){
textView.setText("Logged out");
}
}
};
accessTokenTracker.startTracking();
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, mFacebookCallback);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onResume(){
super.onResume();
Profile profile = Profile.getCurrentProfile();
textView.setText(profile.getName());
}
@Override
protected void onDestroy(){
super.onDestroy();
accessTokenTracker.stopTracking();
}
private void fillTextView(Profile profile){
textView.setText(profile.getName());
}
}
package com.example.arpit.facebooklogindemo;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.facebook.AccessToken;
导入com.facebook.AccessTokenTracker;
导入com.facebook.CallbackManager;
导入com.facebook.facebook回调;
导入com.facebook.facebook异常;
导入com.facebook.FacebookSdk;
导入com.facebook.Profile;
导入com.facebook.login.LoginResult;
导入com.facebook.login.widget.LoginButton;
公共类MainActivity扩展了AppCompatActivity{
CallbackManager CallbackManager;
登录按钮登录按钮;
文本视图文本视图;
AccessTokenTracker AccessTokenTracker;
私有FacebookCallback mFacebookCallback=新FacebookCallback(){
@凌驾
成功时公共无效(LoginResult LoginResult){
Profile Profile=Profile.getCurrentProfile();
if(profile!=null){
fillTextView(配置文件);
}
}
@凌驾
公开作废{
}
@凌驾
public void onError(facebook异常错误){
Log.d(“find”,String.valueOf(error));
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
sdkinInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
textView=(textView)findViewById(R.id.textView);
callbackManager=callbackManager.Factory.create();
accessTokenTracker=新的accessTokenTracker(){
@凌驾
受保护的onCurrentAccessTokenChanged无效(AccessToken oldAccessToken、AccessToken currentAccessToken){
如果(currentAccessToken==null){
setText(“注销”);
}
}
};
accessTokenTracker.startTracking();
loginButton=(loginButton)findviewbyd(R.id.login_按钮);
registerCallback(callbackManager,mFacebookCallback);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
callbackManager.onActivityResult(请求代码、结果代码、数据);
}
@凌驾
受保护的void onResume(){
super.onResume();
Profile Profile=Profile.getCurrentProfile();
textView.setText(profile.getName());
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
accessTokenTracker.stopTracking();
}
私有void fillTextView(纵断面){
textView.setText(profile.getName());
}
}
在设置textView之前,在你的简历上打一个空勾 查看日志中的这一行:
Caused by: java.lang.NullPointerException
at com.example.arpit.facebooklogindemo.MainActivity.onResume(MainActivity.java:83)
我们清楚地提到,由于onResume()
函数中的NullPointerException
,您的应用程序正在崩溃。一个可能的原因是调用profile.getName()
在设置文本之前进行空检查,如下所示:
Profile profile = Profile.getCurrentProfile();
if(null != profile)
textView.setText(profile.getName());
有时,您可能会得到空的或旧的配置文件详细信息。在这种情况下,您需要请求更新配置文件详细信息。下面是示例代码:
Profile profile = Profile.getCurrentProfile();
if(null != profile) {
new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
if (currentProfile != null) {
// handle it
stopTracking();
}
}
}.startTracking();
} else {
textView.setText(profile.getName());
}
很可能没有当前的
配置文件
,对空配置文件调用getName()将导致活动在onResumeYeah中有NPE。它工作了,但现在它在应该显示用户名称的位置显示“注销”,为什么是if(currentAccessToken==null){textView.setText(“注销”)}在登录后而不是在超时登录后被调用,它将返回null或旧的配置文件详细信息。在这种情况下,您需要请求更新详细信息。请参阅我的最新答案。