2016-12-03 52 views
-3

我有一个与AsyncTask有关的问题。我想更改onPostExecute方法中的文本,但它不起作用。我真的不知道我做错了什么。有人可以帮我吗?AsyncTask中的Android setText对UI没有影响

我不明白为什么它在我将AsyncTask声明为嵌套类时起作用,但当我将其声明为自己的类时它不起作用。

这里是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    private Button button = null; 
    private Helper helper; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     this.helper = new Helper(this, getLayoutInflater()); 


     this.button = (Button) findViewById(R.id.button1); 
     this.button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        new MyAsyncTask(helper).execute(""); 
       } catch (Exception e) { 
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
    } 
} 

Helper.java

public class Helper { 

    private Context context; 
    private LayoutInflater inflater; 

    public Helper(Context context, LayoutInflater inflater) { 
     setContext(context); 
     setInflater(inflater); 
    } 

    public Context getContext() { 
     return context; 
    } 

    public void setContext(Context context) { 
     this.context = context; 
    } 

    public LayoutInflater getInflater() { 
     return inflater; 
    } 

    public void setInflater(LayoutInflater inflater) { 
     this.inflater = inflater; 
    } 
} 

MyAsyncTask.java

public class MyAsyncTask extends AsyncTask<String, String, String> { 
    private Helper helper; 

    public MyAsyncTask(Helper helper) { 
     setHelper(helper); 
    } 

    public String getJSON(String url, int timeout) { 
     HttpURLConnection c = null; 
     try { 
      URL u = new URL(url); 
      c = (HttpURLConnection) u.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setRequestProperty("Content-length", "0"); 
      c.setUseCaches(false); 
      c.setAllowUserInteraction(false); 
      c.setConnectTimeout(timeout); 
      c.setReadTimeout(timeout); 
      c.connect(); 
      int status = c.getResponseCode(); 

      switch (status) { 
       case 200: 
       case 201: 
        BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); 
        StringBuilder sb = new StringBuilder(); 
        String line; 
        while ((line = br.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
        br.close(); 
        return sb.toString(); 
      } 

     } catch (MalformedURLException ex) { 
      Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); 
     } finally { 
      if (c != null) { 
       try { 
        c.disconnect(); 
       } catch (Exception ex) { 
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPreExecute() { 

     super.onPreExecute(); 
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
     super.onProgressUpdate(values); 

    } 

    @Override 
    protected void onPostExecute(String s) { 
     TextView t = (TextView) getHelper().getInflater().inflate(R.layout.activity_main, null).findViewById(R.id.textView); 
     t.setText("" + s); //has no effect on the UI but the text was set???? 
     Toast.makeText(getHelper().getContext(), t.getText(), Toast.LENGTH_LONG).show(); 
     super.onPostExecute(s); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     return getJSON("testside.com", 10000); 
    } 

    public Helper getHelper() { 
     return helper; 
    } 

    public void setHelper(Helper helper) { 
     this.helper = helper; 
    } 
} 

谢谢:)

+0

我不知道你在做什么有...如果你想更新UI线程上来看,你应该在UI线程上。AsyncTasks是异步的,因此名称。他们运行在不能访问UI线程的另一个线程上。 –

回答

-1

你的代码看起来不错,但它不起作用,因为它从你膨胀的新layou引用TextView。

你有两个选择:

  1. 在你的助手店的TextView要改变,你可以把它可选的,如果你的助手看起来是这样的:

    public class Helper { 
    
        private Context context; 
        private LayoutInflater inflater; 
        private TextView textView; 
    
        ...  
    
        public void addTextView(TextView textView){ 
         this.texView = textView; 
        } 
    
        public TextView getTextView(){ 
         return textView; 
        } 
    } 
    

    ,然后在你的postExecute()调用TextView t = helper.getTextview()

  2. 通行证的TextView直接向您的AsyncTask所以它看起来像

    public MyAsyncTask(Helper helper, TextView t) { 
        setHelper(helper); 
        this.textView = t; 
    } 
    
+0

非常感谢:)其工作现在:)现在我必须看到我将如何与更多作为一个TextView;)谢谢你:),为什么我得到-1评级? :o – AnKo

+0

当你声明'AsyncTask '第一个参数是你想要输入到asyncTask,所以你可以声明它为'AsyncTask ',然后在'doInBackground(TextView .. .. 。params)'params是你在调用'MyAsync.execute(texview1,textview2)'时传入的textviews数组' –

0

利用您使用单独的文件

class MyActivity{ 
    new MyAsyncTask().execute(); 
} 

你的AsyncTask生活作为其他类扩展的AsyncTask

class MyAsyncTask extends AsyncTask{ 
    public MyAsyncTask(Context appContext){ 

    } 
} 

一定要在你的应用程序的背景下通过的AsyncTask主类之外。您只能执行一次任务的每个实例....但你可以创建并在需要时运行一个新的实例...

会不会飞:

class MyActivity{ 
     MyAsyncTask myTask = new MyAsyncTask(); 
     myTask.execute(); 
     //DO OTHER STUFF 
     myTask.execute(); 
    } 

你会每次都做,而不是需要:

new MyAsyncTask().execute();

+0

对不起,但这不帮助我.. – AnKo

-1

您的AsyncTask是在后台线程上执行。要更新您的用户界面通话,请致电

runOnUiThread(new Runnable() { 

    @Override 
    public void run() { 
     textView.setText("Text"); 
    } 

});