-1

我在连接到本地主机时遇到错误请帮助我的代码是

  public class MainActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
  TextView resultView;
   HttpClient client;
   JSONObject json;
String Dat;
Button btn;
InputStream is=null;
EditText name,roll_no;
String result="";
StringBuilder sb=null;
JSONArray jArray;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    resultView = (TextView) findViewById(R.id.tv1);
    submit();
    btn.setOnClickListener(this);

}
public void onClick(View v){
parse();

}
private String submit() {
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //http post
        try{
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost("http://10.0.2.2:8081/onlineres/login");
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             HttpResponse response = httpclient.execute(httppost);
             HttpEntity entity = response.getEntity();
             is = entity.getContent();
             }catch(Exception e){
                 Log.e("log_tag", "Error in http connection"+e.toString());
            }
        try{
              BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
               sb = new StringBuilder();
               sb.append(reader.readLine() + "\n");

               String line="0";
               while ((line = reader.readLine()) != null) {
                              sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
                }catch(Exception e){
                      Log.e("log_tag", "Error converting result "+e.toString());
                }
    return result;
  }
    public void parse(){
    String roll,name;
    try{
          jArray  = new JSONArray(result);
          JSONObject json_data=null;
          for(int i=0;i<jArray.length();i++){
                 json_data = jArray.getJSONObject(i);
                 name=json_data.getString("Name");
                 roll =json_data.getString("Roll_no");
             }
          }
          catch(JSONException e1){
              Toast.makeText(getBaseContext(), "No City Found"     ,Toast.LENGTH_LONG).show();
          } catch (ParseException e1) {
                e1.printStackTrace();
        }
}
}

和 logcat 是

   07-20 21:23:02.776: I/System.out(306): debugger has settled (1441)
    07-20 21:23:31.564: D/AndroidRuntime(306): Shutting down VM
    07-20 21:23:31.564: W/dalvikvm(306): threadid=1: thread exiting with uncaught exception     (group=0x4001d800)
    07-20 21:23:31.653: E/AndroidRuntime(306): FATAL EXCEPTION: main
    07-20 21:23:31.653: E/AndroidRuntime(306): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.prac/com.example.prac.MainActivity}:     java.lang.NullPointerException
    07-20 21:23:31.653: E/AndroidRuntime(306):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at     android.app.ActivityThread.access$2300(ActivityThread.java:125)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at android.os.Handler.dispatchMessage(Handler.java:99)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at android.os.Looper.loop(Looper.java:123)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at     android.app.ActivityThread.main(ActivityThread.java:4627)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at java.lang.reflect.Method.invoke(Method.java:521)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    07-20 21:23:31.653: E/AndroidRuntime(306):  at dalvik.system.NativeStart.main(Native Method)
   07-20 21:23:31.653: E/AndroidRuntime(306): Caused by: java.lang.NullPointerException
    07-20 21:23:31.653: E/AndroidRuntime(306):  at com.example.prac.MainActivity.onCreate(MainActivity.java:53)
   07-20 21:23:31.653: E/AndroidRuntime(306):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
   07-20 21:23:31.653: E/AndroidRuntime(306):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
   07-20 21:23:31.653: E/AndroidRuntime(306):   ... 11 more

请帮我解决这个问题

4

3 回答 3

1

我猜问题是你打电话给btn.setOnClickListener(this);

我在您的活动中的任何地方都没有看到“btn”初始化。

于 2012-07-20T16:06:44.750 回答
1

你没有初始化按钮。初始化 btn 为:

 ........
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn= (Button) findViewById(R.id.Your_button_id);
        ......
于 2012-07-20T16:08:58.663 回答
0

您的btn变量尚未初始化。在向它添加 onclick 侦听器之前,您必须从视图中获取它。

btn = (Button) findViewById(R.id.btnID);

不过,这需要是您的按钮的 ID,因此只需将其替换为您的按钮 ID。

编辑:忘记投了:)对不起。

于 2012-07-20T16:06:55.690 回答