错误StrictMode $ AndroidBlockGuardPolicy.onNetwork
这个问题在这里已经有了答案:
你必须在MainActivity类中插入2行“StrictMode”,例如下面的例子:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
// JSON here
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(R.layout.activity_main);
Intent intent=new Intent(this,HomeActivity.class);
startActivity(intent);
}
}
Aey.Sakon
最好使用AsyncTask
这里提到的,对于刚刚摆脱的另一种Exception
是使用:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
或这个 :
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
//Your code goes here
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
});
thread.start();
在发生Exception
的活动中发生。
在创建方法后只需添加两行
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
链接地址: http://www.djcxy.com/p/29521.html