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

各位大佬,关于安卓开发中的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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器