各位大佬,关于安卓开发中的Fragment的textview不显示

public class HomePage extends AppCompatActivity {

private TextView mWeather;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.home_page);
    mWeather = findViewById(R.id.weather);

    String coordination = "";
    Location location = MyLocationManager.getLocation(this);
    if (location != null) {
        coordination = location.getLatitude() + "," + location.getLongitude();
    }
    getWeather(Constants.WEATHER_URL + coordination + Constants.WEATHER_API_PARAM);
}

private void getWeather(String requestUrl) {
    @SuppressLint("StaticFieldLeak")
    AsyncTask<String, Void, String> getWeatherTask = new AsyncTask<String, Void, String>() {
        @Override
        protected void onPostExecute(String json) {
            try {
                JSONObject jsonObject = new JSONObject(json);
                JSONObject current = jsonObject.optJSONObject("currently");
                if (current != null) {
                    double fdegree = current.optDouble("temperature");
                    double cdegree = (fdegree - 32) / 1.8f;
                    DecimalFormat decimalFormat = new DecimalFormat("0.0");
                    mWeather.setText("Temperature:" + decimalFormat.format(cdegree) + "c");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected String doInBackground(String... strings) {
            String jsonData = HttpUtils.httpGet(strings[0]);
            return jsonData;
        }
    };
    getWeatherTask.execute(requestUrl);
}

fragment:
public class fragmenthome extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_page, null);
return view;
}
}
home_page 中就有一个名为weather的textview,但是运行项目的时候,home_pag 全是空白,请问各位大神这是怎么回事?

(我现在不知道您的碎片在哪里加载了,在代码中没有体现出来,所以只能判定您的Fragment现在和Activity是相互独立的并没有联系的)
您在Activity和Fragment中使用的XML布局是一样的。
都是home_page.xml。

但是您在Activity中使用AsyncTask对home_page.xml布局中的TextView 进行了更新UI操作,并没有在Fragment中进行UI操作。所以碎片被加载的页面肯定不显示TextView。

我能看看您的代码吗?好让我进一步帮您解决问题。

可能你没有把Fragment放入Activity中,

把View view = inflater.inflate(R.layout.home_page, null);
改成View view = inflater.inflate(R.layout.home_page, container, false);

用代码重新设置下文本控件的大小试试!!

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <fragment
        android:id="@+id/fragment_a"
        android:name="com.example.administrator.a17_fragment.FragmentA"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>

</LinearLayout>

fragment里的textview 你在activity中初始化texview设置文字当然是没有效果的

你把fragment写到activity布局中去 然后初始化fragment 调用fragment中的方法去给里边的textview赋值

你Activity的布局用错了啊,Activity和Fragment用的同一个布局
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.home_page);//这里是Activity的布局,你用的和Fragment的一样,其中有一个必然错了

    View view = inflater.inflate(R.layout.home_page, null);//这里是fragment的布局,和activity里面一样

应该是textview不在fragment的层级里面

如果您的代码中的Fragment已经加载了,但是还是没有出现TextView。
您在Activity中对home_page.xml布局中的TextView进行更新UI操作了。
但是您在Fragment中加载的布局肯定还是原来的home_page.xml,而不是已经被Activity中使用过的并且进行了UI更新的home_page.xml,而是原本的home_page.xml。
home_page.xml布局是布局,被Activity使用过后,还是原本的,而不是被修改过后的,您的Fragment在代码中并没有对home_page.xml布局进行更新UI操作,所以出现的页面是空白。

1.fragment name要配置对,
2.public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

return inflater.inflate(R.layout.xxxx, container, false);;
}
3.TextView 要有值