gpt4 book ai didi

java - 从 Activity 调用 Fragment 方法 - NullPointerException

转载 作者:行者123 更新时间:2023-12-02 00:53:19 24 4
gpt4 key购买 nike

我试图从我的主要 Activity 中调用我的 fragment 中的方法,我得到:

Attempt to invoke virtual method 'void com.example.activity.MyDayFragment.getTaskText()' on a null object reference

主要 Activity :

I have this import:
import android.support.v4.app.Fragment;

public class MainActivity extends AppCompatActivity implements TaskDialog.TaskDialogListener {
public MyDayFragment fragment;

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

Button dialogbutton = findViewById(R.id.dialog_button);
dialogbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});

BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//MyDayFragment fragment = (MyDayFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_myday);

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MyDayFragment()).commit();

}

protected void onStart() {
super.onStart();
MyDayFragment fragment = (MyDayFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_myday);
}

@Override
public void applyText(String task) {
tasktext = task;
fragment.getTaskText();
}

}

fragment :

public void getTaskText(){
//taskText=((MainActivity)getActivity()).getMyData();

}

mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_navigation"></FrameLayout>

<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_menu"/>


<Button
android:id="@+id/dialog_button"
android:layout_width="40dp"
android:layout_height="40dp" />

</RelativeLayout>

FragmentMyday,其中 ID 为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_myday"
android:tag="mydaytag">

<RelativeLayout
android:id="@+id/topll"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView

我不确定从错误中理解什么, fragment 不存在?它还没有创建吗?我已经编辑并添加了 MainActivity 中的 XML 和 onCreate。希望它有所帮助

最佳答案

确实,它有效问题是: 1)变量被声明了2次2)id是“fragment_container”而不是“fragment_myday”

关于java - 从 Activity 调用 Fragment 方法 - NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57841207/

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