我怎样才能解决这个java lang错误?

时间:2014-12-23 05:43:44

标签: java android android-fragments

我正在尝试解决此java lang错误:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sebasdeldihotmail.mediocre11/com.sebasdeldihotmail.mediocre11.SignUpOrLogInActivity}: java.lang.ClassCastException: com.sebasdeldihotmail.mediocre11.SignUpOrLogInActivity cannot be cast to android.app.Activity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2039)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2153)
        at android.app.ActivityThread.access$700(ActivityThread.java:137)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5031)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: com.sebasdeldihotmail.mediocre11.SignUpOrLogInActivity cannot be cast to android.app.Activity
        at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2030)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2153)
            at android.app.ActivityThread.access$700(ActivityThread.java:137)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5031)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)

我已经一次又一次地检查了代码,我无法找到导致应用程序崩溃的原因,也许一些新鲜的眼睛可以帮助找到问题。

这是SignUpOrSignIn类

public class SignUpOrLogInActivity extends Fragment {

    private static final String TAG = "MainFragment";
    Button signIn;
    Button signUp;    

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

        Button signIn = (Button)root.findViewById(R.id.login);
        signIn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
            }
        });

        Button signUp = (Button)root.findViewById(R.id.signup);
        signUp.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),SignUpActivity.class));
            }
        });
        return root;
    }

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

这是布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#67AAE4">

    <Button
        android:id="@+id/signup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sign Up"/>

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"/>


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

清单

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

    <application

        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".ParseStarter">
        <activity
            android:screenOrientation="portrait"
            android:name=".MainActivity"
            android:label="@string/app_name" >

        </activity>

        <activity android:name=".SignUpOrLogInActivity"

            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


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


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


    </application>

</manifest>

&#34;正确的制作&#34;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sebasdeldihotmail.mediocre11" >


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

    <application

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


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


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


    </application>

</manifest>

有人能弄清楚是什么让应用程序崩溃吗?

非常感谢阅读:)

2 个答案:

答案 0 :(得分:3)

SignUpOrLogInActivity延长Fragment。我根据名称假设此类在清单中声明为Activity,这使得Android期望它成为Activity的子类。

Fragment必须包含Activity,因此您需要创建一个扩展Activity并包含Fragment的类(我会将其重命名为不包含“活动”这个词,因为它令人困惑)。您的Android清单应将该活动类声明为启动应用的活动。

答案 1 :(得分:1)

 SignUpOrLogInActivity extends Fragment 

这是一个片段,您将它投射到一个活动。

您需要将其类型化为片段才能使其正常工作

Caused by: java.lang.ClassCastException: com.sebasdeldihotmail.mediocre11.SignUpOrLogInActivity cannot be cast to android.app.Activity