在线程

时间:2017-08-02 23:52:01

标签: android textview real-time

我已经制作了这段代码,我需要在屏幕上实时显示一些值,所以,我试图这样做,有谁知道怎么回事?

public class Test extends AppCompatActivity {

    TextView lastValue;
    int i = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        lastValue = (TextView) findViewById(R.id.textViewID);
        Thread t = new Thread(new Runnable() { 

            @Override
            public void run() {
                while(i<90000) {
                    i++;
                    lastValue.setText(String.valueOf(i));

                }
        }});       t.start();

    }
}

Android监视器:

  

java.lang.NullPointerException:尝试调用虚方法&#39; int   android.text.Layout.getLineCount()&#39;在null对象引用上                                                          在android.widget.TextView.onMeasure(TextView.java:7520)                                                          在android.view.View.measure(View.java:19917)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)                                                          在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1485)                                                          在android.widget.LinearLayout.measureVertical(LinearLayout.java:775)                                                          在android.widget.LinearLayout.onMeasure(LinearLayout.java:657)                                                          在android.view.View.measure(View.java:19917)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)                                                          在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                          在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)                                                          在android.view.View.measure(View.java:19917)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)                                                          在android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)                                                          在android.view.View.measure(View.java:19917)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)                                                          在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                          在android.view.View.measure(View.java:19917)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)                                                          在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1485)                                                          在android.widget.LinearLayout.measureVertical(LinearLayout.java:775)                                                          在android.widget.LinearLayout.onMeasure(LinearLayout.java:657)                                                          在android.view.View.measure(View.java:19917)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)                                                          在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                          在com.android.internal.policy.DecorView.onMeasure(DecorView.java:729)                                                          在android.view.View.measure(View.java:19917)                                                          在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2436)                                                          在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2182)                                                          在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1366)                                                          在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6768)                                                          在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:923)                                                          在android.view.Choreographer.doCallbacks(Choreographer.java:735)                                                          在android.view.Choreographer.doFrame(Choreographer.java:667)                                                          在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:909)                                                          在android.os.Handler.handleCallback(Handler.java:761)                                                          在android.os.Handler.dispatchMessage(Handler.java:98)                                                          在android.os.Looper.loop(Looper.java:156)                                                          在android.app.ActivityThread.main(ActivityThread.java:6523)                                                          at java.lang.reflect.Method.invoke(Native Method)                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:941)                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

3 个答案:

答案 0 :(得分:2)

关于你的代码的第一点,你不能从UI线程之外修改View对象。因此你应该使用各种处理程序或技术来完成它,就像@Embydextrous答案一样

第二点,如果您的代码因为这个原因而失败,这是UI线程,则logchat将是这样的

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6566)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:911)

但是你的logchat是非常不同的。问题将是另一回事,是否都是日志?可能错误的原因是你的布局,检查它

答案 1 :(得分:0)

您要做的是从线程访问视图。但View仅适用于UI线程,因此抛出空指针异常。为此,您可以尝试:

Thread t = new Thread(new Runnable() { 
    @Override
    public void run() {
        while(i<90000) {
            i++;
            runOnUiThread(new Runnable {
                lastValue.setText(String.valueOf(i));
            });
        }
    }
});       
t.start();

runOnUiThread导致线程在UI线程上运行。

答案 2 :(得分:0)

在Android的开发中,UI界面控件中显示的所有内容都在一个单独的线程UI-Thread(或称为MainThread);当你想刷新其他线程中的接口时,必须跳转到UIThread,你可以使用Handler来实现这个操作。

public class Test extends AppCompatActivity {

private static final int REFRESH_LASTVALUE=0;
private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case REFRESH_LASTVALUE:
                lastValue.setText((String)msg.obj);
                break;
        }
    }
};
TextView lastValue;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    lastValue = (TextView) findViewById(R.id.textViewID);
    new Thread(new Runnable() {
        @Override
        public void run() {
            while(i<90000) {
                i++;
                Message message=new Message();
                message.what=REFRESH_LASTVALUE;
                message.obj=String.valueOf(i);
            }
        }}).start();

}

}

或者您可以使用runOnUiThread,也可以通过Handler,Message等实现。