gpt4 book ai didi

java - Android Studio 中的 Fragment 不稳定

转载 作者:行者123 更新时间:2023-12-01 23:30:35 25 4
gpt4 key购买 nike

我是使用 Android Studio 的完全初学者。在此代码中,我尝试使用工具栏中强加的按钮来实现 fragment 。基本上,我想在单击添加按钮时显示 fragment 内容。我想从 fragment 中获取用户的一些输入,将其更新为Firebase 并从 Firebase 接收它并将其检索到回收者 View 。当我尝试执行下面给出的代码时, fragment 闪烁,然后应用程序停止工作。任何应用我的流程的替代方案将不胜感激。

这是我的 Activity 文件..



package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;


public class history extends AppCompatActivity {



enter code here

EditText success;
private FrameLayout frameLayout;
private historyfrag historyfrag;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);


Toolbar toolbar = findViewById(R.id.toolbarhistory);
setSupportActionBar(toolbar);


ActionBar ab =getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);

frameLayout = findViewById(R.id.framelayut);

historyfrag = new historyfrag();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.toolbarhistorymenu,menu);
return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item){
switch (item.getItemId()) {

case R.id.print:
Toast.makeText(getApplicationContext(), "Print", Toast.LENGTH_LONG).show();
return true;

case R.id.add:
setupfragment(historyfrag);
return true;

default:
return super.onOptionsItemSelected(item);


}



}

private void setupfragment(Fragment fragment) {

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.framelayut,fragment);
fragmentTransaction.commit();


}


}

这是 Activity 的 xml 文件...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbarhistory"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorAccent"
android:elevation="6dp"
app:menu="@menu/toolbarhistorymenu"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="History" />

<FrameLayout
android:id="@+id/framelayut"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbarhistory" />

</RelativeLayout>

在Logcat中发现这个错误

    java.lang.RuntimeException: com.example.myapplication.history@2fb40c4 must implement OnFragmentInteractionListener
at com.example.myapplication.historyfrag.onAttach(historyfrag.java:85)
at androidx.fragment.app.Fragment.performAttach(Fragment.java:2574)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:828)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1197)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1080)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:119)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

最佳答案

将类名称更改为:

public class history extends AppCompatActivity implements YourFragmentClassName.OnFragmentInteractionListener

在 fragment 类中你应该有:

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

关于java - Android Studio 中的 Fragment 不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58291950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com