Fragment 中 TextView 的空对象引用

null object reference of TextView in Fragment

我有两个片段和一个主要片段activity。第一个片段 FriendsFragment(扩展 ListFragment)显示在屏幕上,当用户单击一个项目时,主要 Activity 将 FriendsFragment 替换为 FeedFragment,然后从 FeedFragment 调用一个方法来更新 textView。

尽管我使用 findViewById 方法进行实例化,但我收到一个错误,提示 FeedFragment class 中的 textView 对象为空。

我查看了相关问题并尝试了解决方案,但没有任何效果。我试过 getView().findViewById(R.id.feed_view)getActivity().findViewById(R.id.feed_view),我试过将它们放在 onActivityCreated()onCreateView()

唯一可行的是在 onActivityCreated() 中编写此代码:

text = getView().findViewById(R.id.feed_view); 
text.setText("some string"); 

但这不是我想要的

FeedFragments.java

public class FeedFragment extends Fragment {
    private static String TAG = "Feed Fragment";
    private TextView text;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.i(TAG, "Entered FeedFragment.java onActivityCreated()");
        super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    Log.i(TAG, "Entered FeedFragment.java onCreateView()");
    View v = inflater.inflate(R.layout.fragment_feed, container,false);;
    text = v.findViewById(R.id.feed_view);
    return v;
}

public void updateDisplay(int position)
{
    Log.i(TAG, "FeedFragment.java: updateDisplay()");
    text.setText(position);
}

MainActivity.java

// previously declared feedFragment: feedFragment = new FeedFragment(); 
public void onItemSelected(int position)
{
    Log.i(TAG, "Entered onItemSelected(" + position + ")");
    fragManager = getFragmentManager();
    FragmentTransaction fragTransaction = fragManager.beginTransaction();
    fragTransaction.replace(R.id.fragment_container, feedFragment, "feed_fragment");
    fragTransaction.addToBackStack(null);
    fragTransaction.commit();

    feedFragment.updateDisplay(position);
}

fragment_feed.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <TextView
        android:id="@+id/feed_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/greeting" />

</ScrollView>

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

onViewcreated() 在 updateDisplay() 之后被调用,因为系统不会立即 运行 它。

您需要在创建 feedFragment 时将一个值传递给它,并将其存储在一个包中,并在系统 运行 片段内部代码后检索它。

你的解决方法是这样的:

当您实例化 feedFragment 时,您可以这样做:

feedFragment = FeedFragment.newInstance(position)

在 FeedFragment 中 class 你应该有一个静态代码:

private static final String ARG_PARAM1 = "param1";

public static FeedFragment newInstance(int param1) {
    FeedFragment fragment = new FeedFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PARAM1, param1);
    fragment.setArguments(args);
    return fragment;
}

和非静态代码:

private int mParam1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getInt(ARG_PARAM1);
    }
} 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    Log.i(TAG, "Entered FeedFragment.java onCreateView()");
    View v = inflater.inflate(R.layout.fragment_feed, container,false);;
    text = v.findViewById(R.id.feed_view);
    text.setText(String.valueOf(mParam1));
    return v;
}

我将 mParam1 包装在 String.valueOf() 中,因为当您将整数传递给 setText 时,它认为您尝试使用 Strings.xml 资源而不是您选择的数字。

我还使用了非常通用的变量名。请将它们更改为有意义的内容,以便您的代码有意义。