我遇到了致命的异常:android中的main和emulator上没有执行的应用程序?
`我遇到了致命的异常:android中的main和emulator上没有执行的应用程序?,android,android-emulator,Android,Android Emulator,` 异常的基础似乎来自“FileOutputStream.write”, 您是否已将写入权限添加到清单中 public class JsonExampleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(
异常的基础似乎来自“FileOutputStream.write”, 您是否已将写入权限添加到清单中
public class JsonExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_example);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://codeincloud.tk/json_android_example.php");
TextView textView = (TextView)findViewById(R.id.textView1);
try {
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONObject object = new JSONObject(jsonResult);
String name = object.getString("name");
String verion = object.getString("version");
textView.setText(name + " - " + verion);
}
catch (JSONException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null)
answer.append(rLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
return answer;
}
}`
当您尝试从无法执行的线程更新视图时,会发生此错误。您需要从onPostExecute更新视图,或者使用runOnUiThread更新视图 您可以按如下方式使用runOnUiThread:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
您是如何确定的?您正在主(UI)线程上进行网络I/O。这已经是件坏事了。您的POST请求可能需要很长时间才能执行,同时您已经阻止了主线程。正如@David所建议的,您应该创建一个新线程,从中执行网络I/O操作(考虑使用AsyncTask),也许您可以使用stacktrace发布错误,以便我们可以看到问题所在。
。。。onPostExecute或use runOnUiThread…
我在这里没有看到任何其他线程
或异步任务
。。。
runOnUiThread(new Runnable() {
public void run() {
// your code to update the UI thread here
}
});