刷新线程中的editText
我已经制作了这段代码,并且我需要实时显示屏幕上的一些值,所以我试图这样做,有人知道怎么回事吗?
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:尝试在android.view.View的android.widget.TextView.onMeasure(TextView.java:7520)上的空对象引用上调用虚拟方法'int android.text.Layout.getLineCount()'。测量(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.view.View.measure(View.java:19917)android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)在android.android.widget.LinearLayout.onMeasure(LinearLayout.java:657) .widget.FrameLayout.onMeasure(FrameLayout.java:185)在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)在android.view.View.measure(View.java:19917)在机器人。 view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)a 在android.view上的android.widget.FrameLayout.onMeasure(FrameLayout.java:185)处的android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)处的android.view.View.measure(View.java:19917)。 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.view.View.measure(View.java:19917)at com.android.internal.policy.DecorView.onMeasure(DecorView.java:729)处的android.widget.FrameLayout.onMeasure(FrameLayout.java:185)at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2436)at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2182)at android.view.ViewRootImpl.doTraversal(ViewRootImpl.ja VA:1366)在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6768)在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:923)在android.view.Choreographer.doCallbacks(Choreographer.java :735)at android.view.Choreographer.doFrame(Choreographer.java:667)at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:909)at 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)在java.lang。 reflect.Method.invoke(本地方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:941)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
你试图做的是从一个线程访问一个视图。 但视图仅在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线程上运行。
关于你的代码的第一点,你不能从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)
但是你的聊天记录是非常不同的。问题将会是另一回事,是否所有的日志? 可能错误的原因是你的布局,检查它
在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等实现的。
链接地址: http://www.djcxy.com/p/84733.html上一篇: Refresh editText in a thread
下一篇: Error inflating class com.elyeproj.loaderviewlibrary.LoaderImageViewImageView