- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在按照 android 教程构建天气应用程序,但是当我尝试将 mTemperature TextView 设置为我从预测 API 获得的温度时,我收到此错误“android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original创建 View 层次结构的线程可以触及它的 View 。”。当我登录以查看所有天气详细信息时,代码确实运行良好。有人可以提出解决方案吗?我必须提到我正在使用第 3 方 API 进行名为 OkHTTP 的异步 HTTP 调用。
这是我在 MainActivity 中的代码:
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
private TextView mTemperature;
private CurrentWeather mCurrentWeather;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-----------MY CODE STARTS HERE------------------
mTemperature = (TextView)findViewById(R.id.temp);
String API_KEY = "API_KEY";
//aberdeen, coordinates
double latitude = 57.1531;
double longtitude = -2.0928;
String forecast = "https://api.forecast.io/forecast/"+ API_KEY +"/"+ latitude+","+ longtitude;
if(isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecast)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {a
}
//when the call to the Okhttp library finishes, than calls this method:
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
//Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mCurrentWeather = getCurrentDetails(jsonData);
//do sth with the forecast details
mTemperature.setText(mCurrentWeather.getTemperature()+"");
} else {
alertUserAboutError();
}
} catch (IOException e) {
Log.e(TAG, "Exception caught:", e);
}
catch (JSONException e){
Log.e(TAG, "Exception caught:", e);
}
}
});
}else{
//Toast.makeText(this,getString(R.string.network_unavailable_message),Toast.LENGTH_LONG).show();
WIFIDialogFragment dialog = new WIFIDialogFragment();
dialog.show(getFragmentManager(), getString(R.string.error_dialog_text));
}
Log.d(TAG, "Main UI code is running!");
}
//throws JSONException, doing it like that, we place the
// responsability of handaling this exeption to the caller of the method
private CurrentWeather getCurrentDetails(String jsonData) throws JSONException{
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
Log.i(TAG,"From JSON: " + timezone);
JSONObject currently = forecast.getJSONObject("currently");
CurrentWeather currentWeather = new CurrentWeather();
currentWeather.setHumidity(currently.getDouble("humidity"));
currentWeather.setTime(currently.getLong("time"));
currentWeather.setIcon(currently.getString("icon"));
currentWeather.setPrecipChange(currently.getDouble("precipProbability"));
currentWeather.setSummery(currently.getString("summary"));
currentWeather.setTemperature(currently.getDouble("temperature"));
currentWeather.setTimeZone(timezone);
Log.d(TAG,currentWeather.getFormattedTime());
return currentWeather;
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
//contition to check if there is a network and if the device is connected
if(networkInfo != null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
private void alertUserAboutError() {
AlertDIalogFragment dialog = new AlertDIalogFragment();
dialog.show(getFragmentManager(),getString(R.string.error_dialog_text));
}
这是来自 logcat 的完整日志:
05-16 03:25:59.941 5348-5370/koemdzhiev.com.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: OkHttp Dispatcher
Process: koemdzhiev.com.stormy, PID: 5348
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874)
at android.view.View.requestLayout(View.java:17476)
at android.view.View.requestLayout(View.java:17476)
at android.view.View.requestLayout(View.java:17476)
at android.view.View.requestLayout(View.java:17476)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
at android.view.View.requestLayout(View.java:17476)
at android.widget.TextView.checkForRelayout(TextView.java:6871)
at android.widget.TextView.setText(TextView.java:4057)
at android.widget.TextView.setText(TextView.java:3915)
at android.widget.TextView.setText(TextView.java:3890)
at koemdzhiev.com.stormy.MainActivity$1.onResponse(MainActivity.java:67)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
最佳答案
尝试从非 UI 线程的任何线程访问 UI 元素时会发生此错误。
要从非 UI 线程
访问/修改元素,请使用 runOnUIThread
关于android.view.ViewRootImpl$CalledFromWrongThreadException,OkHttp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30272983/
您好,我正在尝试学习 Android 开发,并且正在构建一个新应用程序。我已经实现了一个 ListFragment 和一个适配器来显示一些注释。注释按我的意愿显示,但现在当我单击一个项目时,我无法触发
嗨,我正在为我的应用程序进行内存分析。为此,我正在尝试识别我的应用程序的内存泄漏。我得到以下类型的内存泄漏。 com.sample.activityname has leaked: * static
我在 ListView 中显示了网页下载百分比的进度条。 for(int i=0;i { int id; public UploadTask(int id) {
我有一种感觉是我的计时器导致了这次崩溃。奇怪的是我似乎无法在模拟器中重现它,但我的 Play 控制台崩溃报告告诉我用户不断遇到此崩溃: android.view.ViewRootImpl$Called
我使用的是 this , 显示来自互联网的图像,但它会引发如下错误: 04-12 13:45:05.337: E/AndroidRuntime(27897): Caused by: android.v
我已经用其他设备测试了我的应用程序。它运行良好,但是当我在我的 上安装它时三星盖乐世 S8 .它没有给我任何 UI 更改,但从 logcat,我可以看到 /ViewRootImpl: sendUser
我是 Android 初学者,所以如果这是一个愚蠢的问题,请友善。 我正在尝试动态更新四个 TextView。每当我尝试更新它们时,程序就会崩溃。 我不明白“只有创建 View 层次结构的原始线程才能
我正在按照 android 教程构建天气应用程序,但是当我尝试将 mTemperature TextView 设置为我从预测 API 获得的温度时,我收到此错误“android.view.ViewRo
我正在为一个 Android 应用开发一个测试类,我收到了一个 android.view.ViewRootImpl$CalledFromWrongThreadException。该代码假设模拟输入到文
我试图在 ViewRootImpl.java 中创建一个带有循环器和处理程序的线程,但收到此错误: 02-03 14:04:14.266 884 884 E AndroidRuntime: j
我刚开始使用 Xamarin。我主要使用 Console.WriteLine() 调试一些小值。这工作正常,但 ViewRootImpl 日志真的很烦人。如果您按下一个按钮,它会显示它的当前状态。 [
06-13 17:29:30.750: W/dalvikvm(6257): threadid=13: thread exiting with uncaught exception (group=0xb
每当我触摸我的应用程序设备上的屏幕时,日志都会显示: D/ViewRootImpl: ViewPostImeInputStage processPointer 0 D/ViewRootImpl: Vi
只是玩了一下(我确实知道 View.getRootView()),我手动地逐步上升到根甚至更远。 ... View tv=(View)mv.getParent(); View uv=(View)tv.
我在 fragment = ViewRootImpl: sendUserActionEvent() mView == null 内的微调器中遇到此错误,有人知道如何解决吗? ,提前致谢! 最佳答案 当
我不确定我是否理解这两者之间的关系。据我了解,ViewRootImpl 持有 mView,它是每个窗口的主视图,而 DecorView 也是每个窗口的主视图? 最佳答案 事实证明 ViewRootIm
我目前正在构建一个只能在平板电脑上运行的 Android 应用程序。一项功能是在(自定义创建的)对话框中显示事件列表。 在某些情况下,当用户确认这些事件时,对话框不会被垃圾回收,并且在多次打开对话框后
我有一个非常奇怪的 NullPointerException(仅发生在 Android 4.1 上,既不在 ICS 上也不在 Jelly Bean 4.2 上)编辑:仅在 Samsung Touch
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Android - ViewRootImpl$CalledFromWrongThreadException
我有一个像这样的 cart_layout: 以及 Cart.java 中的 java 代码: protected void onCreat
我是一名优秀的程序员,十分优秀!