android 工作室中的 Facebook activity 出错

Error with facebook activity in android studio

我正在尝试开发基本的 facebook 活动,该应用程序将获取用户名并将其显示在屏幕上,但我遇到了一种致命异常:主要错误我提供了从清单文件开始的完整代码集,class 文件,xml 文件和 Logcat 错误,非常需要您的帮助

下面提供的代码是我的清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.world.trialwithmugaputhagam">

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

<application
    android:name=".MyApplicatioon"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <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>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    <activity android:name=".MainFragment" />
</application>

下面的代码是 class 文件 我有 used.In 下面的代码 我也分配了 mTextDetails但是当我将光标悬停时,它说该值未分配

import android.os.Bundle;
import android.app.Fragment;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.AccessToken;
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 Fragment {

private TextView mTextDetails;
private CallbackManager mcallbackManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    mcallbackManager=CallbackManager.Factory.create();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_main,container,false);
}


private FacebookCallback<LoginResult> mcallback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();
        if (profile != null){
            mTextDetails.setText("Welcome "+ profile.getName());
        }
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException error) {

    }
};



@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    LoginButton loginButton = (LoginButton)    view.findViewById(R.id.login_button);
    loginButton. setReadPermissions("user_friends");
    loginButton. setFragment(this);
    loginButton.registerCallback(mcallbackManager, mcallback);
}

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

下面的代码是我的XML文件

<RelativeLayout 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: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.world.trialwithmugaputhagam.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Welcome"
    android:id="@+id/text_details"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_above="@+id/login_button"/>

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="30dp"
    android:layout_centerInParent="true"/></RelativeLayout>

Logcat error 抱歉对齐错误。

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.world.trialwithmugaputhagam/com.example.world.trialwithmugaputhagam.MainActivity}: java.lang.ClassCastException: com.example.world.trialwithmugaputhagam.MainActivity cannot be cast to android.app.Activity
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
                                                                                       at android.app.ActivityThread.access0(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.ClassCastException: com.example.world.trialwithmugaputhagam.MainActivity cannot be cast to android.app.Activity
                                                                                       at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
                                                                                       at android.app.ActivityThread.access0(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)

因为您要从 Fragment 扩展 MainActivity。因此,您的 MainActivity 实际上不是 Activity,而是 Fragment。从 ActivityActivity 的子类扩展它。

像这样:

MainActivity extends AppCompatActivity

编辑 1:(基于评论):

您还没有初始化mTextDetails; 尝试在 ActivityonCreate:

中这样做

mTextDetails = (TextView)findViewById(R.id.text_view_id);