首页 > 解决方案 > 当我在个人资料片段活动上按下我的可点击文本时,我的应用程序不断崩溃

问题描述

这是我的 logcat,因为我按下 TextView 后应用程序崩溃

06-04 12:58:26.944 5680-5680/com.example.sandwichswitch E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sandwichswitch, PID: 5680
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.sandwichswitch/com.example.sandwichswitch.fragmentprofile}; have you declared this activity in your AndroidManifest.xml?
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
    at android.app.Activity.startActivityForResult(Activity.java:3917)
    at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
    at android.app.Activity.startActivityForResult(Activity.java:3877)
    at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:560)
    at android.app.Activity.startActivity(Activity.java:4200)
    at androidx.core.content.ContextCompat.startActivity(ContextCompat.java:251)
    at androidx.fragment.app.FragmentHostCallback.onStartActivityFromFragment(FragmentHostCallback.java:166)
    at androidx.fragment.app.Fragment.startActivity(Fragment.java:1377)
    at androidx.fragment.app.Fragment.startActivity(Fragment.java:1365)
    at com.example.sandwichswitch.fragmentprofile.onClick(fragmentprofile.java:44)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是活动的java部分(fragmentprofile活动)

package com.example.sandwichswitch;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class fragmentprofile extends Fragment implements View.OnClickListener{
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull @org.jetbrains.annotations.NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {

    View view =inflater.inflate(R.layout.fragmentprofile,container,false);
    return view;


}

@Override
public void onActivityCreated(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    TextView textView=getActivity().findViewById(R.id.tvtest);

    textView.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.tvtest:
        Intent intent=new Intent(getActivity(),fragmentprofile.class);
        startActivity(intent);
    }
    }
}

这是fragmentprofile的xml部分

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvtest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:text="profile"
        android:textSize="30dp"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是清单,我添加了它,因为日志猫说了一些关于在这里声明我的活动但如果你继续滚动你可以看到我尝试了什么以及结果是什么

        <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.sandwichswitch">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".createprofile"></activity>
            <activity android:name=".

Profile" />
        <activity android:name=".LogInScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

日志猫说关于在那里声明活动但是当我添加该行时

<activity android:name="fragmentprofile"
        tools:ignore="Instantiatable"></activity>

它仍然崩溃,这是 logcat

06-04 13:14:50.370 6273-6273/com.example.sandwichswitch E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sandwichswitch, PID: 6273
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sandwichswitch/com.example.sandwichswitch.fragmentprofile}: java.lang.ClassCastException: com.example.sandwichswitch.fragmentprofile cannot be cast to android.app.Activity
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.ClassCastException: com.example.sandwichswitch.fragmentprofile cannot be cast to android.app.Activity
    at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

标签: androidandroid-studioandroid-layoutandroid-fragments

解决方案


问题在下一节:

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.tvtest:
            Intent intent=new Intent(getActivity(),fragmentprofile.class);
            startActivity(intent);
    }
}

特别是在下一行的最后一个参数中:

Intent intent=new Intent(getActivity(),fragmentprofile.class);

方法startActivity顾名思义,用于启动活动,而不是片段。代码中的最后一个参数是传递fragmentprofile.class,这是错误的。相反,它应该是您希望开始的活动课程。

如果您需要打开片段,请阅读片段管理器文档,因为打开片段与启动活动是完全不同的范例。


推荐阅读