首页 > 解决方案 > Fragment中TextView的空对象引用

问题描述

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

我收到一个错误,即 FeedFragment 类中的 textView 对象为空,即使我使用该findViewById方法进行实例化。

我查看了相关问题并尝试了解决方案,但没有任何效果。我试过做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" />

标签: javaandroidandroid-fragmentstextview

解决方案


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

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

你解决它的方式是这样的:

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

feedFragment = FeedFragment.newInstance(position)

在 FeedFragment 类中,您应该有一个静态代码:

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 资源而不是您选择的数字。

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


推荐阅读