0

我试图从我的主要活动中调用我的片段中的一个方法,我得到:

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

主要活动:

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();
    }

}

片段:

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

    }

主要活动.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>

其中 ID 为 FragmentMyday:

<?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

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

4

1 回答 1

0

事实上,IT WORKS 问题是:1) 变量被声明了 2 次 2) id 是“fragment_container”而不是“fragment_myday”

于 2019-09-09T09:00:22.130 回答