Android Facebook SDK登录例外

时间:2014-11-10 11:10:01

标签: login android-facebook facebook-sdk-3.0

我正在关注官方android SDK的教程,并尝试编写一个简单的登录程序。 所以我做的是......

   <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.logintutorial"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="20" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id"/>

   </manifest>

MainActivity只使用片段事务。

  import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;

 import android.os.Bundle;
 import android.support.v4.app.FragmentActivity;
 import android.view.Menu;

 public class MainActivity extends FragmentActivity {

  private MainFragment mainFragment;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        mainFragment = new MainFragment();
        getSupportFragmentManager()
        .beginTransaction()
        .add(android.R.id.content, mainFragment)
        .commit();
    } else {
        // Or set the fragment from restored state info
        mainFragment = (MainFragment) getSupportFragmentManager()
        .findFragmentById(android.R.id.content);
       }
     }
   }

在UiLifecyleHelper和Session.StatusCallBack类的帮助下,MainFragment类用于会话管理。

 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 com.facebook.Session;
 import com.facebook.SessionState;
 import com.facebook.UiLifecycleHelper;
 import com.facebook.widget.LoginButton;

 public class MainFragment extends Fragment {

 private static final String TAG = "MainFragment";

  private UiLifecycleHelper uiHelper;
 private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(final Session session, final SessionState state, final Exception exception)       
{
        onSessionStateChange(session, state, exception);
    }
};

    @Override
    public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_main, container, false);

    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
    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);
}

@Override
public void onResume() {
    super.onResume();

    // For scenarios where the main activity is launched and user
    // session is not null, the session state change notification
    // may not be triggered. Trigger it if it's open/closed.
    Session session = Session.getActiveSession();
    if (session != null &&
            (session.isOpened() || session.isClosed()) ) {
        onSessionStateChange(session, session.getState(), null);
    }

    uiHelper.onResume();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
  }
}

也是主要活动的xml文件......

<LinearLayout 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:orientation="vertical">

<com.facebook.widget.LoginButton
    android:id="@+id/authButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    />

   </LinearLayout>

最后我将app id作为

放在string.xml文件中
 <?xml version="1.0" encoding="utf-8"?>
 <resources>

   <string name="app_name">Login Tutorial</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
    <string name="app_id">XXX6999249XXXXX</string>
   </resources>

当我运行它时,我得到以下异常。

  11-10 13:01:46.784: E/AndroidRuntime(31157): FATAL EXCEPTION: main
  11-10 13:01:46.784: E/AndroidRuntime(31157): java.lang.RuntimeException: 
  Unable to start activity  
  ComponentInfo{com.example.logintutorial/com.example.logintutorial.MainActivity}:
  java.lang.NullPointerException: Argument 'applicationId' cannot be null

1 个答案:

答案 0 :(得分:1)

元数据标签必须位于应用程序标签内,而不是外部!将清单更改为:

<?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.logintutorial"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="20" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id"/>
    </application>

   </manifest>