Java 从Activity-NullPointerException调用片段方法
我试图调用我的主要活动片段中的一个方法,我得到:Java 从Activity-NullPointerException调用片段方法,java,android,nullpointerexception,Java,Android,Nullpointerexception,我试图调用我的主要活动片段中的一个方法,我得到: 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 AppComp
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();
}
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
事实上,它是有效的
问题是:
1) 变量声明了2次
2) id是“fragment\u container”而不是“fragment\u myday”看起来像是getSupportFragmentManager
从onStart
调用时无法findframentbyid
。我想是这样的,id是正确的,但我已经检查了很多次(当我开始编写时,它也会自动完成。您是在XML中静态添加片段还是在代码中添加片段?发布您的活动布局和onCreate
方法。这可能有助于获得答案。这是不正确的:MyDayFragment fragment=(MyDayFragment)getSupportFragmentManager()。findFragmentById(R.id.fragment_myday)
。您不应该将片段
声明为局部变量。事实上,所有已注释掉的尝试都犯了相同的错误。MyDayFragment fragment=…
–将类型放在它前面会声明一个与字段完全不同的新局部变量。从beg中删除MyDayFragment
那一局。你还需要修正R.id
。(接得好,@StephenC.)