簡體   English   中英

當我在配置文件片段活動上按下可點擊文本時,我的應用程序一直崩潰

[英]my app keeps crashing the moment I press my clickable text on the profile fragment activity

這是我的 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) 

問題在下一節:

@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這是錯誤的。 相反,它應該是您希望開始的活動 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();

試試這個調用片段到片段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM