当我在个人资料片段活动中按下可点击的文本时,我的应用程序就一直崩溃



这是我按下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);
}
}
}

这是碎片配置文件的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) 

问题在下一节:

@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,这是错误的。相反,它应该是一个你想要开始的活动类。

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

您正在同一个片段中尝试启动活动

你的片段名称是fragmentprofile,你是像这个一样的呼叫意图

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

你调用相同的片段,然后它会崩溃一个apk,解决方案是

Fragment2 fragment2 = new Fragment2();  //your destination fragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content, fragment2);
fragmentTransaction.commit();

尝试这个调用片段到片段

相关内容

  • 没有找到相关文章