无法让我的 TextView 在片段中更新

Can't get my TextView to update in a fragment

我能想到的都试过了,我也在Stack中尝试了很多解决方案。似乎没有一个能解决我的问题。我正在尝试传递我从较早的意图(已解析的 JSON 数据)传递的字符串。每次调用 set text 时都会出现 Null Pointer Exception,我尝试使用一种方法来设置文本以及仅设置它。任何帮助将不胜感激。

这是我要更新的片段。

public class HomeFragment extends Fragment{
TextView tv;
public HomeFragment(){}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    LayoutInflater lf = getActivity().getLayoutInflater();

    View rootView = lf.inflate(R.layout.fragment_home, container, false);
    tv  = (TextView) rootView.findViewById(R.id.accountName);
    tv.setText("test");
    return rootView;
}

}

我只是放了一个普通的字符串来测试。这是 XML.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/lblCompany"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="71dp" />
</RelativeLayout>

提前感谢您提供的任何帮助。

这是在 xml 中为您的 TextView 设置的 ID:

android:id="@+id/lblCompany"

这是您在onCreateView中搜索的ID:

tv = (TextView) rootView.findViewById(R.id.accountName);

ID 需要匹配,您才能更新特定 TextView 中的文本。尝试

tv = (TextView) rootView.findViewById(R.id.lblCompany);

相反。