fatesses 2018-04-24 06:52 采纳率: 50%
浏览 3120
已采纳

各位大佬,关于安卓开发中的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 全是空白,请问各位大神这是怎么回事?

  • 写回答

10条回答 默认 最新

  • 暖暖的云阳 2018-04-24 08:48
    关注

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

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog