尽管f1
现在应该包含一个FragmentA
实例,为什么它仍然为空。
MainActivity.java
public class MainActivity extends AppCompatActivity implements FragmentA.Communicator {
FragmentManager manager;
FragmentA f1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.main_fragment_container);
if (fragment == null) {
fragment = new FragmentA();
manager.beginTransaction()
.add(R.id.main_fragment_container, fragment)
.commit();
}
f1 = (FragmentA) manager.findFragmentById(R.id.main_fragment_container);
f1.setCommunicator(this);
}
@Override
public void changeText(String text) {
FragmentB f2 = (FragmentB) manager.findFragmentById(R.id.details_fragment_container);
if (f2 != null && f2.isVisible()) {
f2.updateText(text);
} else {
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("text", text);
startActivity(intent);
}
activity_main.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_fragment_container" />
</LinearLayout>
FragmentA.Java
public class FragmentA extends Fragment {
Communicator communicator;
ListView listView;
public FragmentA() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_a, container, false);
listView = view.findViewById(R.id.listview);
return view;
}
public void setCommunicator(Communicator communicator) {
this.communicator = communicator;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("first row");
arrayList.add("second row");
arrayList.add("third row");
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1,
arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
communicator.changeText(arrayList.get(position));
}
});
}
public interface Communicator {
void changeText(String text);
}
fragment_a.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentA">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listview" />
</FrameLayout>
Log: `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.example.workout.FragmentA.setCommunicator(com.example.workout.FragmentA$Communicator)' on a
null object reference`
答案 0 :(得分:1)
commit()
是异步的。如果您希望片段可以通过commitNow()
立即可用,则应使用findFragmentById
。
当然,就您而言,您可以将fragment
和f1
变量组合在一起:
f1 = manager.findFragmentById(R.id.main_fragment_container);
if (f1 == null) {
f1 = new FragmentA();
manager.beginTransaction()
.add(R.id.main_fragment_container, f1)
.commit();
}
// f1 is now never null
f1.setCommunicator(this);