访问片段内部的TextView会产生NullPointerException



片段定义如下。我正在尝试设置positionTextView的文本。

public class Fragment2 extends Fragment {
private TextView positionTextView,fragment2TextView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =  inflater.inflate(R.layout.layout_fragment2, container, false);
positionTextView = view.findViewById(R.id.positionTextView);
fragment2TextView = view.findViewById(R.id.fragment2TextView);
Toast.makeText(getContext(),"OnCreateView",Toast.LENGTH_LONG).show();
return  view;
}
public void setContentOfTextView( int position) {
positionTextView.setText(Integer.toString(position));
}
}

MainActivity中,我添加Fragment2如下。

@Override
public void fragmentRespond(int index) {
FragmentTransaction transaction =  fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
transaction.add(R.id.group2,fragment2,"fragment2");
transaction.commit();
transaction.addToBackStack("fragment2");
fragment2.setContentOfTextView(index);
}

我在另一个包含ListView的片段中有fragmentRespond函数。ListViewonItemClickListener调用此函数,即fragmentRespond(int index)

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] quotes = getResources().getStringArray(R.array.quotes);
CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(getContext(),quotes,null);
listView.setAdapter(customArrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
fragmentCommunicator.fragmentRespond(position);
}
});
}

我得到以下错误

java.lang.NullPointerException:尝试在null对象引用上调用虚拟方法"void android.widget.TextView.setText(java.lang.CharSequence(">

我们非常感谢您对这个问题或改进编码风格的任何建议。

提前谢谢。

当您尝试使用活动中的setContentOfTextView函数设置文本时,由于尚未加载Fragment的视图,因此您将获得一个NPE。

你的片段中的视图需要首先膨胀,以便在其中加载一些东西。因此,你应该在你的片段的onCreateView函数中这样做。

关于从活动中传递信息,您可以在从活动中启动片段时,使用传递给片段的Bundle轻松完成此操作。

以下是如何做到这一点。

在您的活动中,在启动片段时使用Bundle传递数据。

@Override
public void fragmentRespond(int index) {
// Prepare the bundle
Bundle bundle = new Bundle();
bundle.putInt("index", index);
FragmentTransaction transaction =  fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragment2.setArguments(bundle); // Set the arguments here to be passed on to your fragment while loading.
transaction.add(R.id.group2,fragment2, "fragment2");
transaction.commit();
transaction.addToBackStack("fragment2");
}

在您的片段中,从Bundle中获取所需的数据,并将其加载到TextView中。

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =  inflater.inflate(R.layout.layout_fragment2, container, false);
positionTextView = view.findViewById(R.id.positionTextView);
fragment2TextView = view.findViewById(R.id.fragment2TextView);
// Load the information in your TextView here
Bundle args = getArguments();
if (args != null && args.containsKey("index")) {
setContentOfTextView(args.getInt("index"));
}
Toast.makeText(getContext(),"OnCreateView",Toast.LENGTH_LONG).show();
return  view;
}

我希望这能有所帮助。

相关内容

最新更新