求各位大神给看看
- Java code
Activityprivate Button mButton1,mButton2; private TextView mTextView1; private JoyHttp j;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*透过findViewById建构子建立TextView与Button对象*/ mButton1 =(Button) findViewById(R.id.myButton1); mButton2 =(Button) findViewById(R.id.myButton2); mTextView1 = (TextView) findViewById(R.id.myTextView1); /*设定OnClickListener来聆听OnClick事件*/ mButton1.setOnClickListener(new Button.OnClickListener() { /*重写onClick事件*/ @Override public void onClick(View v) { /*声明网址字符串*/ String uriAPI = "http://localhost:8080"; String message = "123";// post(uriAPI); mTextView1.setText(j.post(uriAPI,message)); } }); Postpublic String post(String uriAPI, String message) { String fal = "fal"; HttpPost httpRequest = new HttpPost(uriAPI); try { /* 发出HTTP request */ httpRequest.setEntity(new StringEntity(message)); // httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); /* 取得HTTP response */ HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); /* 若状态码为200 ok */ if (httpResponse.getStatusLine().getStatusCode() == 200) { /* 取出响应字符串 */ String strResult = EntityUtils.toString(httpResponse.getEntity()); // mTextView1.setText(strResult); return strResult; } // else // { // mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString()); // } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return fal; }
log
09-24 15:39:47.529: E/AndroidRuntime(14810): FATAL EXCEPTION: main
09-24 15:39:47.529: E/AndroidRuntime(14810): java.lang.RuntimeException: Unable to start activity ComponentInfo{irdc.EX08_01/irdc.EX08_01.EX08_01}: java.lang.NullPointerException
09-24 15:39:47.529: E/AndroidRuntime(14810): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
09-24 15:39:47.529: E/AndroidRuntime(14810): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
09-24 15:39:47.529: E/AndroidRuntime(14810): at android.app.ActivityThread.access$600(ActivityThread.java:127)
09-24 15:39:47.529: E/AndroidRuntime(14810): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
09-24 15:39:47.529: E/AndroidRuntime(14810): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 15:39:47.529: E/AndroidRuntime(14810): at android.os.Looper.loop(Looper.java:137)
09-24 15:39:47.529: E/AndroidRuntime(14810): at android.app.ActivityThread.main(ActivityThread.java:4511)
09-24 15:39:47.529: E/AndroidRuntime(14810): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 15:39:47.529: E/AndroidRuntime(14810): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 15:39:47.529: E/AndroidRuntime(14810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
09-24 15:39:47.529: E/AndroidRuntime(14810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
09-24 15:39:47.529: E/AndroidRuntime(14810): at dalvik.system.NativeStart.main(Native Method)
09-24 15:39:47.529: E/AndroidRuntime(14810): Caused by: java.lang.NullPointerException
09-24 15:39:47.529: E/AndroidRuntime(14810): at irdc.EX08_01.EX08_01.onCreate(EX08_01.java:50)
09-24 15:39:47.529: E/AndroidRuntime(14810): at android.app.Activity.performCreate(Activity.java:4470)
09-24 15:39:47.529: E/AndroidRuntime(14810): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)