3

片段定义如下。我正在尝试将文本设置为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);
}

fragmentRespond在另一个包含ListView. 的调用这个函数,即ListViewonItemClickListenerfragmentRespond(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:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

非常感谢有关此问题或改进编码风格的任何建议。

先感谢您。

4

1 回答 1

2

您正在获得 NPE,因为当您尝试使用活动中的函数Fragment设置文本时,尚未加载视图。setContentOfTextView

您的片段中的视图需要首先膨胀,以便在其中加载一些东西。因此,您应该在片段的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;
}

我希望这会有所帮助。

于 2020-03-06T19:54:55.137 回答