![](/img/trans.png)
[英]NullPointerException when calling a Fragment method from Activity?
[英]Calling Fragment Method from Activity - 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 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>
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。希望對您有所幫助
確實,它的工作原理是:1)將變量聲明為2次2)id為“ fragment_container”而不是“ fragment_myday”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.