Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 返回类型片段线程无效_Android_Multithreading_Personality Insights - Fatal编程技术网

Android 返回类型片段线程无效

Android 返回类型片段线程无效,android,multithreading,personality-insights,Android,Multithreading,Personality Insights,我已经在Android IDK中创建了一个线程,为什么它说返回类型无效! 我只想显示从Profile 我想更改片段中的文本 Thread thread= new Thread(){ if (getActivity()!=null) { getActivity().runOnUiThread(new Runnable() { @Override public

我已经在Android IDK中创建了一个线程,为什么它说返回类型无效! 我只想显示从
Profile
我想更改片段中的文本

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();
试试这个

    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();
新建异步任务(){
私有字符串json;
受保护的空位背景(空位…空位){
Profile Profile=service.getProfile(文本);
Gson Gson=新的Gson();
json=gson.toJson(profile);
返回null;
}
受保护的void onPostExecute(void结果){
setText(json);
}
}.execute();
试试这个

    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();
新建异步任务(){
私有字符串json;
受保护的空位背景(空位…空位){
Profile Profile=service.getProfile(文本);
Gson Gson=新的Gson();
json=gson.toJson(profile);
返回null;
}
受保护的void onPostExecute(void结果){
setText(json);
}
}.execute();

它在哪里说“返回类型无效”?还有,你为什么还需要这条线?如果您希望在后台线程上运行
getProfile
,则可能需要查看AsyncTask。我尝试使用Async task,但后来放弃了,
if(getActivity()!=null)
这里是这样说的。如果您可以在异步任务中使用它,则对我的@DennisKOk没有问题,我将发布AsyncTask的可能解决方案。但是你需要学习这个行业的工具。在android中直接使用线程并不是一件小事,如果您尝试,可能会导致更多问题。它在哪里说“返回类型无效”?还有,你为什么还需要这条线?如果您希望在后台线程上运行
getProfile
,则可能需要查看AsyncTask。我尝试使用Async task,但后来放弃了,
if(getActivity()!=null)
这里是这样说的。如果您可以在异步任务中使用它,则对我的@DennisKOk没有问题,我将发布AsyncTask的可能解决方案。但是你需要学习这个行业的工具。在android中直接使用线程并不是件小事,如果您尝试,可能会导致更多问题。对不起,省略了
()
。现在试试它需要doInBackground()参数,我不在开发人员电脑上。已修复。您也可以使用IDE的自动建议来实现方法。该方法看起来不错,但抛出
java.net.UnknownHostException:无法解析主机“gateway.watsonplatform.net”:没有与主机名关联的地址
好吧,这是您的服务无法连接。您没有提供此代码,如果您自己无法解决此问题,则可能是一个新问题。抱歉,省略了
()
。现在试试它需要doInBackground()参数,我不在开发人员电脑上。已修复。您也可以使用IDE的自动建议来实现方法。该方法看起来不错,但抛出
java.net.UnknownHostException:无法解析主机“gateway.watsonplatform.net”:没有与主机名关联的地址
好吧,这是您的服务无法连接。您没有提供这段代码,如果您自己无法理解,这可能是一个新问题。