我遵循教程:http://www.portalandroid.org/comunidade/viewtopic.php?f=7&t=185&view=previous
我的代码:
package com.example.jeneves;
import java.util.Arrays;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LoginButton;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
/**
* A placeholder fragment containing a simple view.
*/
public class Login extends Fragment {
Button btnEnviar;
EditText edtPost;
TextView txtName;
ImageView imgAvatar;
private static final String TAG = "Login";
private UiLifecycleHelper uiHelper;
private Session session;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login, container, false);
imgAvatar = (ImageView) view.findViewById(R.id.imgAvatar);
txtName = (TextView) view.findViewById(R.id.textUserName);
edtPost = (EditText) view.findViewById(R.id.postMsg);
btnEnviar = (Button) view.findViewById(R.id.btnEnviar);
LoginButton authButton = (LoginButton) view.findViewById(R.id.login_button);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
};
@Override
public void onResume() {
super.onResume();
if (session.isOpened()) {
Log.i(TAG, "Logged in...");
} else if (session.isClosed()) {
Log.i(TAG, "Logged out...");
}
uiHelper.onResume();
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
我的Log Cat错误:
04-17 18:07:58.854: D/AndroidRuntime(14047): Shutting down VM
04-17 18:07:58.854: W/dalvikvm(14047): threadid=1: thread exiting with uncaught exception (group=0x41f87ba8)
04-17 18:07:58.864: E/AndroidRuntime(14047): FATAL EXCEPTION: main
04-17 18:07:58.864: E/AndroidRuntime(14047): Process: com.example.jeneves, PID: 14047
04-17 18:07:58.864: E/AndroidRuntime(14047): java.lang.RuntimeException: Unable to resume activity {com.example.jeneves/com.example.jeneves.MainActivity}: java.lang.NullPointerException
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.os.Handler.dispatchMessage(Handler.java:102)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.os.Looper.loop(Looper.java:136)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-17 18:07:58.864: E/AndroidRuntime(14047): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 18:07:58.864: E/AndroidRuntime(14047): at java.lang.reflect.Method.invoke(Method.java:515)
04-17 18:07:58.864: E/AndroidRuntime(14047): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-17 18:07:58.864: E/AndroidRuntime(14047): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-17 18:07:58.864: E/AndroidRuntime(14047): at dalvik.system.NativeStart.main(Native Method)
04-17 18:07:58.864: E/AndroidRuntime(14047): Caused by: java.lang.NullPointerException
04-17 18:07:58.864: E/AndroidRuntime(14047): at com.example.jeneves.Login.onResume(Login.java:73)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.support.v4.app.Fragment.performResume(Fragment.java:1543)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1894)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:466)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:455)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.support.v7.app.ActionBarActivity.onPostResume(ActionBarActivity.java:115)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.app.Activity.performResume(Activity.java:5323)
04-17 18:07:58.864: E/AndroidRuntime(14047): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
04-17 18:07:58.864: E/AndroidRuntime(14047): ... 12 more
答案 0 :(得分:0)
似乎onResume()中的session var是罪魁祸首。
答案 1 :(得分:0)
会话未初始化。你应该在onResume()之前初始化它。
答案 2 :(得分:0)
试试这个:
public void onSaveInstanceState(Bundle savedInstanceState) {
if (savedInstanceState != null)
{
super.onSaveInstanceState(savedInstanceState);
}
uiHelper.onSaveInstanceState(savedInstanceState);
}