Android:由于Facebook登录,应用程序在启动时崩溃了?

时间:2014-02-27 19:44:16

标签: android login startup

我已经创建了一个facebook登录活动,但我没有遇到编译错误,但当我在设备上安装该应用程序时,它无法打开,并说不幸的是应用程序已停止工作...

以下是主要活动:

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

public class MainActivity extends FragmentActivity{

    private MainFragment mainFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        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);
        }

    }


}

这是主要片段:

import com.facebook.SessionState;
import com.facebook.Session;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LoginButton;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;

public class MainFragment extends Fragment{

    private static final String TAG = "MainFragment";
    private UiLifecycleHelper uiHelper;

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

        LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
        authButton.setFragment(this);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
    }

    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...");
        }
    }

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

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

        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);
    }

}

这是清单:

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

    <uses-sdk 
        android:minSdkVersion = "2"
        android:targetSdkVersion= "3"
        android:maxSdkVersion = "4" />

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

    <uses-permission android:name="android.permission.INTERNET"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.facebook.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>

</manifest>

这是logcat:

02-27 14:51:46.065: E/AndroidRuntime(20554): FATAL EXCEPTION: main
02-27 14:51:46.065: E/AndroidRuntime(20554): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joshuaphilpott.studybuddy/com.facebook.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.facebook.MainActivity" on path: /data/app/com.joshuaphilpott.studybuddy-1.apk
02-27 14:51:46.065: E/AndroidRuntime(20554):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at android.app.ActivityThread.access$700(ActivityThread.java:165)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at android.os.Looper.loop(Looper.java:137)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at android.app.ActivityThread.main(ActivityThread.java:5455)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at java.lang.reflect.Method.invoke(Method.java:525)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at dalvik.system.NativeStart.main(Native Method)
02-27 14:51:46.065: E/AndroidRuntime(20554): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.facebook.MainActivity" on path: /data/app/com.joshuaphilpott.studybuddy-1.apk
02-27 14:51:46.065: E/AndroidRuntime(20554):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
02-27 14:51:46.065: E/AndroidRuntime(20554):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2220)
02-27 14:51:46.065: E/AndroidRuntime(20554):    ... 11 more

2 个答案:

答案 0 :(得分:1)

在清单文件中添加两件事:

<activity android:name="com.facebook.LoginActivity" ></activity>

<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>

答案 1 :(得分:0)

在您的AndroidManifest中,您已声明MainActivity位于package com.facebook。我假设情况并非如此,因此您应该使用com.facebook名称替换package,或者因为AndroidManifest会添加隐式package名称如果没有,你可以简单地使用:

 <activity
        android:name=".MainActivity"
        android:label="@string/app_name">

。在前面告诉AndroidManifest在开始时附加您的package名称