Facebook 集成错误 - NullPointerException

Posted

技术标签:

【中文标题】Facebook 集成错误 - NullPointerException【英文标题】:Facebook Integration Error - NullPointerException 【发布时间】:2014-06-02 08:05:09 【问题描述】:

我正在尝试将 Facebook SDK 与我的应用程序集成,但我无法修复 NullPointer 异常。

我正在学习教程: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);
     



我的日志猫错误:

    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

【问题讨论】:

【参考方案1】:

会话未初始化。你应该在 onResume() 之前初始化它。

【讨论】:

嗯,我应该在 OnCreate 或 onCreateView 上这样做。抱歉,我对所有这些新概念有点困惑。 两者都可以,但 onCreate() 将是理想的,因此您可以对其进行一次初始化。确保在初始化之前进行空检查。 嗯。刚刚制作: if(Session.openActiveSession(getActivity(), this, false, callback) == null) Session.setActiveSession(session); 还是不行。【参考方案2】:

似乎 onResume() 中的会话变量是罪魁祸首。

【讨论】:

是的,好像在OnResume()里,我刚开始学习,但是还看不懂。 它展示了如何在教程中进一步初始化它。【参考方案3】:

试试这个:

public void onSaveInstanceState(Bundle savedInstanceState) 

    if (savedInstanceState != null) 
    
      super.onSaveInstanceState(savedInstanceState);
    
    uiHelper.onSaveInstanceState(savedInstanceState);
 

【讨论】:

以上是关于Facebook 集成错误 - NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

Facebook 集成错误 - NullPointerException

facebook集成代码设计错误

Facebook 集成的应用程序配置错误[重复]

facebook集成错误2500 OAuthException xcode 8 Swift 3

将 Facebook SDK 与 Xcode Swift 项目集成 - AppDelegate 错误?

Facebook 集成到 Android 中?