简体   繁体   English

从Activity调用Fragment方法-NullPointerException

[英]Calling Fragment Method from Activity - NullPointerException

I'm trying to call a method which is in my fragment from my main activity and I get: 我试图从我的主要活动中调用一个片段中的方法,我得到:

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

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

}

the fragment: 片段:

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

    }

mainactivity.xml 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 where the ID is: 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

I'm not sure what to understand from the error, that the fragment doesn't exist? 我不确定从错误中了解什么,该片段不存在? that it's not created yet? 它还没有创建? I have edited and added the XML and onCreate from the MainActivity.Hope it helps 我已经从MainActivity编辑并添加了XML和onCreate。希望对您有所帮助

确实,它的工作原理是:1)将变量声明为2次2)id为“ fragment_container”而不是“ fragment_myday”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM