簡體   English   中英

試圖在片段中顯示textview

[英]trying to display textview in fragment

是的,我知道有人問過我,但我的問題始終是null我嘗試了所有方法,請嘗試PLZ幫助,錯誤消息:java.lang.NullPointerException:嘗試調用虛擬方法'void android.widget.TextView.setText(java.lang.CharSequence) '在空對象引用上

public class InfoFragment extends Fragment {

    private TextView tvInfo;

    public  InfoFragment(){};
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.main_fragment, container, false);

        tvInfo = rootView.findViewById(R.id.tv_info);
        tvInfo.setText("hello");

        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"
    android:orientation="vertical">

        <TextView
            android:id="@+id/tv_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

</RelativeLayout>

您顯示的代碼是正確的。 它應該正確地做到100%。 但是,也許您正在誇大其他布局,這就是為什么TextView為null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM