,虽然我收到了错误。我正在使用android Studio和java,并希望获取该名称:值对并将TextView设置为等于该值对。
我认为主要的问题是我可能会使用名称:JSON中的值对错误,我也可能错误地更改了TextView。
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.v("s","--onResponse successfully called.");
String mmessage = response.body().string();
Log.v("s", "--Response: " + mmessage);
try {
JSONObject json = new JSONObject(mmessage);
String api_info_balance = json.getString("balance");
String api_info_pending_charges = json.getString("pending_charges");
String api_info_last_payment_date = json.getString("last_payment_date");
String api_info_last_payment_amount = json.getString("last_payment_amount");
_balancetextview.setText(api_info_balance.toString());
_pending_chargestextview.setText(api_info_pending_charges.toString());
_last_payment_datetextview.setText(api_info_last_payment_date.toString());
_last_payment_amounttextview.setText(api_info_last_payment_amount.toString());
}catch(Exception e) {
e.printStackTrace();
Log.v("s", e.toString());
}
}
{“balance”:“ - 23.66”,“pending_charges”:“0.00”,“last_payment_date”:“2017-01-27 14:52:13”,“last_payment_amount”:“ - 50.00”}
logcat的:
[ 11-11 15:34:08.755 16120:16186 D/ ]
SurfaceInterface::setAsyncMode: set async mode 1
11-11 15:34:08.762 16120-16186/com.example.sss.lesdothis D/EGL_emulation: eglMakeCurrent: 0x9c98f8a0: ver 2 0 (tinfo 0xa8a8b7e0)
11-11 15:34:20.640 16120-16120/com.example.sss.lesdothis V/s: --button clicked.
11-11 15:34:22.142 16120-16627/com.example.sss.lesdothis V/s: --onResponse successfully called.
11-11 15:34:22.142 16120-16627/com.example.sss.lesdothis V/s: --Response: {"balance":"-23.66","pending_charges":"0.00","last_payment_date":"2017-01-27 14:52:13","last_payment_amount":"-50.00"}
11-11 15:34:22.143 16120-16627/com.example.sss.lesdothis W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-11 15:34:22.144 16120-16627/com.example.sss.lesdothis W/System.err: at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7286)
11-11 15:34:22.144 16120-16627/com.example.sss.lesdothis W/System.err: at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1155)
11-11 15:34:22.144 16120-16627/com.example.sss.lesdothis W/System.err: at android.view.View.requestLayout(View.java:21922)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis I/chatty: uid=10082(u0_a82) i.vultr.com/... identical 5 lines
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err: at android.view.View.requestLayout(View.java:21922)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err: at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:1959)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err: at android.view.View.requestLayout(View.java:21922)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err: at android.widget.TextView.checkForRelayout(TextView.java:8526)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err: at android.widget.TextView.setText(TextView.java:5392)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err: at android.widget.TextView.setText(TextView.java:5248)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err: at android.widget.TextView.setText(TextView.java:5205)
11-11 15:34:22.145 16120-16627/com.example.sss.lesdothis W/System.err: at com.example.sss.lesdothis.MainActivity$1$1.onResponse(MainActivity.java:113)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis W/System.err: at java.lang.Thread.run(Thread.java:764)
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis V/s: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-11 15:34:22.146 16120-16627/com.example.sss.lesdothis V/s: --pgld1
答案 0 :(得分:1)
您应该仅在堆栈跟踪状态下更新ui线程中的ui。如果您正在参加活动,可以通过调用runOnUiThread
方法来执行此操作:
runOnUiThread(new Runnable() {
@Override
public void run() {
_balancetextview.setText(api_info_balance.toString());
_pending_chargestextview.setText(api_info_pending_charges.toString());
_last_payment_datetextview.setText(api_info_last_payment_date.toString());
_last_payment_amounttextview.setText(api_info_last_payment_amount.toString());
}
});
基本上只需在调用此方法时包装UI更新,它就不会再崩溃了。如果您想了解更多信息,请点击link to the docs。