我一直不明白 就這幾行代碼 單獨在一個工程可以,就這麼復制黏貼就爆空指針異常,我真的,已經陷入無限循環兩天了
求大神指點迷津
上代碼
public class Personlogin1 extends Activity {
private HttpClient client = null;
private Button on_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_personlogin1);
on_login = (Button) findViewById(R.id.onlogin);
on_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
readNet("http://www.weather.com.cn/adat/cityinfo/101010100.html");
}
public void readNet(String url) {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... arg0) {
System.out.println("1");
String urlString = arg0[0];
System.out.println("2");
HttpGet get = new HttpGet(urlString);
try {
System.out.println("3");
HttpResponse response = client.execute(get);
System.out.println("4");
String value = EntityUtils.toString(response.getEntity());
System.out.println("44444444"+value);
System.out.println("5");
return value;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
System.out.println("6");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("7");
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
System.out.println(result);
}
}.execute(url);
}
});
代碼就這些 ![圖片說明](http://img.ask.csdn.net/upload/201601/25/1453723473_824864.png)
大哥 你的 client 為null 又沒有實例化,下面就直接執行execute 方法,所以直接報null 出錯了
private HttpClient client = null;
HttpResponse response = client.execute(get);