Reputation: 11
I have made this code and i need to show in realtime some values on the screen, so, I was trying to do it like this, does anyone know how what is wrong?
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 Monitor:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getLineCount()' on a null object reference at android.widget.TextView.onMeasure(TextView.java:7520) at android.view.View.measure(View.java:19917) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1485) at android.widget.LinearLayout.measureVertical(LinearLayout.java:775) at android.widget.LinearLayout.onMeasure(LinearLayout.java:657) at android.view.View.measure(View.java:19917) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) at android.view.View.measure(View.java:19917) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139) at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393) at android.view.View.measure(View.java:19917) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.view.View.measure(View.java:19917) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1485) at android.widget.LinearLayout.measureVertical(LinearLayout.java:775) at android.widget.LinearLayout.onMeasure(LinearLayout.java:657) at android.view.View.measure(View.java:19917) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at com.android.internal.policy.DecorView.onMeasure(DecorView.java:729) at android.view.View.measure(View.java:19917) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2436) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2182) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1366) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6768) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:923) at 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) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
Upvotes: 0
Views: 1793
Reputation: 1
In the development of Android, all displayed in the UI interface controls are in a separate thread UI-Thread (or called MainThread); when you want to refresh the interface in the other thread, must jump to UIThread, you can use Handler to achieve this operation.
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();
}
}
or you can use runOnUiThread,that is also achieved through Handler, Message, and so forth.
Upvotes: 0
Reputation: 1661
What you are trying to do is accessing a view from a thread. But the View is only available with UI Thread and hence null pointer exception is thrown. To achieve this you can try:
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
causes thread to run on UI Thread.
Upvotes: 0
Reputation: 406
First point about your code,you cannot modify View objects from outside the UI thread.Therefore you should use various handlers or techniques to accomplish it ,Like as @Embydextrous answers
second point ,if your code fails for this reason which is UI thread, logchat will be like this
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)
But your logchat is very different.Problem will be something else,Is it all logs ? probably errors reason is your layout ,check it
Upvotes: 2