2015-05-15 82 views
-4

我想在一段时间内更新textView。即文本随时间变化而变化。内容存储在一个数组中,但是当我尝试从一个线程执行时,该应用程序崩溃。这里是源代码:执行线程时,应用程序崩溃了,Android?

import android.content.Intent; 
import android.graphics.Typeface; 
import android.net.Uri; 
import android.support.v4.app.DialogFragment; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.Window; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.util.Calendar; 


public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    final MessageList sample = new MessageList(); 
    sample.MessageMethod(); 

    final Calendar c = Calendar.getInstance(); 




    new Thread(new Runnable() { 
     @Override 
     public void run() 
     { 
      int x = 0; 

      while (i<1) 
      { 
       if(c.get(Calendar.MINUTE)%2==0) 
       { 
        ++x; 
        TextView alertMessage = 

(TextView)findViewById(R.id.messages); 
        String parse = sample.message[x]; 
        alertMessage.setText(parse); 

       } 

      } 



     } 
    }).start(); 

该应用程序使用一个线程之前已成功工作,我不知道这是否在初始化是由于一个错误。有什么建议么?

+3

安置自己的logcat输出 –

+0

其工作正常,现在在模拟器,但是,没有TextView出现在TextView中 –

+0

你在哪里声明我? –

回答

3

你必须在UI Thread运行这部分代码:

TextView alertMessage = (TextView)findViewById(R.id.messages); 
String parse = sample.message[x]; 
alertMessage.setText(parse); 

它应该是这样的:

new Thread(new Runnable(){ 
    @Override 
    public void run(){ 
    int x=0; 
     while(i<1){ 
      if(c.get(Calendar.MINUTE)%2==0){ 
      ++x; 
      runOnUiThread(new Runnable(){ 
      @Override 
      public void run(){ 
       TextView alertMessage=(TextView)findViewById(R.id.messages); 
       String parse=sample.message[x]; 
       alertMessage.setText(parse); 
      } 
     }); 
     } 
} 
} 
}).start(); 
+0

String parse = sample.message [x];这里我得到一个错误:变量'x'是从内部类访问的,需要声明为final,但是当我声明它为final时,++ x不会工作,因为它不能为最终变量赋值,所以任何想法解决这个问题? :( –

+0

在'run'方法中声明'x',然后创建另一个变量。将创建的变量赋值给x并使用它。 – hrskrs

0

你不能比UI线程其他线程更新UI。

TextView alertMessage = 

(TextView)findViewById(R.id.messages); 
        String parse = sample.message[x]; 

alertMessage.setText(parse);这条线是问题。

地下1

runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      TextView alertMessage = 

(TextView)findViewById(R.id.messages); 
        String parse = sample.message[x]; 
        alertMessage.setText(parse); 

     } 
    }); 

你只能从更新UI线程的UI元素取代的TextView的代码的两行。或者您也可以拨打此行的帮助下runOnUIThread

你的代码会变得,像这样下面

new Thread(new Runnable() { 
     @Override 
     public void run() { 
      int x = 0; 

      while (i < 1) { 
       if (c.get(Calendar.MINUTE) % 2 == 0) { 
        ++x; 
        final String parse = sample.message[x]; 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          TextView alertMessage = 

          (TextView) findViewById(R.id.messages); 
          alertMessage.setText(parse); 
         } 
        }); 
       } 
      } 

     } 
    }).start(); 
+1

我想你的意思是'你不能从UI线程以外的线程更新UI' ? –

+0

谢谢,更新了答案 – AAnkit

+0

String parse = sample.message [x];这里我得到一个错误:变量'x'从内部类中访问,需要声明为final,但是当我声明它是final时, ++ x,不会工作,因为它不能将值赋给最终变量,所以任何想法来解决这个问题?:( –

0
final Handler handler = new Handler(); 

new Thread(new Runnable() { 
     @Override 
     public void run() 
     { 
      int x = 0; 

      while (i<1) 
      { 
       if(c.get(Calendar.MINUTE)%2==0) 
       { 
        ++x; 
       handler.post(new Runnable() { 
        @Override 
        public void run() { 
        TextView alertMessage = (TextView)findViewById(R.id.messages); 
        String parse = sample.message[x]; 
        alertMessage.setText(parse); 
        } 
       }); 

       } 

      } 



     } 
    }).start();