Invalid return type fragment thread

删除回忆录丶 提交于 2019-12-11 16:44:42

问题


I have created a Thread in Android IDK why it says invalid return type! All I want is to display the json I get from Profile I want to change the text in my fragment

Thread thread= new Thread(){
          if (getActivity()!=null)
          {
              getActivity().runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      Profile profile = service.getProfile(text);
                      Gson gson = new Gson();
                      String json = gson.toJson(profile);
                      output.setText(json.toString());
                  }
              });
          }
      };
        thread.start();

回答1:


Try this

    new AsyncTask<Void, Void, Void> (){
        private String json;

        protected Void doInBackground(Void... voids) {
            Profile profile = service.getProfile(text);
            Gson gson = new Gson();
            json = gson.toJson(profile);
            return null;
        }

        protected void onPostExecute(Void result) {
            output.setText(json);
        }
    }.execute();


来源:https://stackoverflow.com/questions/49987736/invalid-return-type-fragment-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!