Android 错误:无法启动活动 ComponentInfo..?

Posted

技术标签:

【中文标题】Android 错误:无法启动活动 ComponentInfo..?【英文标题】:Android Error: Unable to start activity ComponentInfo..? 【发布时间】:2016-01-11 19:03:45 【问题描述】:

我是 android 新手,并尝试使用 facebook sdk 使用 Android studio 在 android 中添加 Facebook 登录,但我收到此错误 这是 logcat,

01-11 13:38:36.244 5656-5656/com.example.arpit.facebooklogindemo E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.arpit.facebooklogindemo/com.example.arpit.facebooklogindemo.MainActivity: android.view.InflateException: Binary XML file line #18: Error inflating class com.facebook.login.widget.LoginButton
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
   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: android.view.InflateException: Binary XML file line #18: Error inflating class com.facebook.login.widget.LoginButton
   at android.view.LayoutInflater.createView(LayoutInflater.java:620)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
   at com.example.arpit.facebooklogindemo.MainActivity.onCreate(MainActivity.java:26)
   at android.app.Activity.performCreate(Activity.java:5133)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
   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.reflect.InvocationTargetException
   at java.lang.reflect.Constructor.constructNative(Native Method)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
   at android.view.LayoutInflater.createView(LayoutInflater.java:594)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
   at com.example.arpit.facebooklogindemo.MainActivity.onCreate(MainActivity.java:26) 
   at android.app.Activity.performCreate(Activity.java:5133) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
   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: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.
   at com.facebook.internal.Validate.sdkInitialized(Validate.java:136)
   at com.facebook.AccessTokenTracker.<init>(AccessTokenTracker.java:55)
   at com.facebook.login.widget.LoginButton$2.<init>(LoginButton.java:561)
   at com.facebook.login.widget.LoginButton.configureButton(LoginButton.java:561)
   at com.facebook.FacebookButtonBase.<init>(FacebookButtonBase.java:66)
   at com.facebook.login.widget.LoginButton.<init>(LoginButton.java:200)
   at java.lang.reflect.Constructor.constructNative(Native Method) 
   at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
   at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
   at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
   at com.example.arpit.facebooklogindemo.MainActivity.onCreate(MainActivity.java:26) 
   at android.app.Activity.performCreate(Activity.java:5133) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
   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) 
01-11 13:43:37.044 5656-5656/com.example.arpit.facebooklogindemo I/Process: Sending signal. PID: 5656 SIG: 9

这里是 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>

这里是activity_main.xml,

<?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_
    android:layout_
    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_
        android:layout_
        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_
        android:layout_
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_
        android:layout_
        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>

这里是 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 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;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FacebookSdk.sdkInitialize(getApplicationContext());

        callbackManager = CallbackManager.Factory.create();

        loginButton = (LoginButton)findViewById(R.id.login_button);

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() 
            @Override
            public void onSuccess(LoginResult loginResult) 
                Profile profile = Profile.getCurrentProfile();
                if(profile != null)
                    textView = (TextView)findViewById(R.id.textView);
                    textView.setText(profile.getName());
                
            

            @Override
            public void onCancel() 

            

            @Override
            public void onError(FacebookException error) 

            
        );
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    

【问题讨论】:

尝试在setContentView(R.layout.activity_main);之前调用FacebookSdk.sdkInitialize(getApplicationContext()); 【参考方案1】:

你还没有初始化 FacebookSDK

根据Caused by: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.

这里推荐的方法是创建一个应用程序类并在那里初始化 SDK:

public class MyApp extends Application 

    @Override
    public void onCreate() 
        super.onCreate();

        FacebookSdk.sdkInitialize(getApplicationContext());


并更新您的 AndroidManifest:

<?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:name=".MyApp"
        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>

【讨论】:

以上是关于Android 错误:无法启动活动 ComponentInfo..?的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio:无法启动活动组件信息

当我有意启动它时,为啥android无法识别第二个活动?

无法识别启动活动:升级到 Android Studio 4.0 后未找到默认活动

Android Java.Lang.RuntimeException:无法启动活动组件信息

无法识别启动活动,未找到默认活动启动活动时出错

Android - 无法开始活动