Я получаю NullPointerException
при попытке позвонить Fragment.onActivityResult()
. Как правильно вызвать этот метод в HomeFragment?
(navigation_calendar
is HomeFragment's
id. Это также subfragment
из "nav_host_fragment
").
я получаю сообщение об ошибке:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.fragment.app.Fragment.onActivityResult(int, int, android.content.Intent)' on a null object reference
at com.example.jobbkalender.MainActivity.onActivityResult(MainActivity.java:59)
MainActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment homeFragment = getSupportFragmentManager().findFragmentById(R.id.navigation_calendar);
if(requestCode == DELETE_EVENT) {
homeFragment.onActivityResult(requestCode,resultCode,data);
}
}
Домашний фрагмент:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final ListView eventListView = getView().findViewById(R.id.listViewEventList);
String date = dateToString(day,selectedMonth,selectedYear);
List<WorkdayEvent> events = searchEvents(date);
EventListAdapter eventListAdapter = new EventListAdapter(getContext(),0,events);
eventListView.setAdapter(eventListAdapter);
}
EventListAdaper:
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putSerializable("EVENT",event);
Intent intent = new Intent(mContext, ViewEvent.class);
intent.putExtra("EVENTBUNDLE",bundle);
Activity activity = (Activity)mContext;
activity.startActivityForResult(intent,DELETE_EVENT);
}
});
РЕДАКТИРОВАТЬ: XML для nav_host_fragment:
<navigation 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:id="@+id/mobile_navigation"
app:startDestination="@+id/navigation_calendar">
<fragment
android:id="@+id/navigation_calendar"
android:name="com.example.jobbkalender.ui.home.HomeFragment"
android:label="@string/app_name"
tools:layout="@layout/fragment_home" />
</navigation>