Android应用程序无法运行并显示以下错误

时间:2012-07-20 16:03:35

标签: android

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

  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

请帮助我解决这个问题。

3 个答案:

答案 0 :(得分:1)

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

我没有在您的活动中的任何位置初始化“btn”。

答案 1 :(得分:1)

您没有初始化按钮。将btn初始化为:

 ........
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn= (Button) findViewById(R.id.Your_button_id);
        ......

答案 2 :(得分:0)

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

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

这需要是您按钮的ID,所以只需将其替换为您的按钮ID即可。

编辑:忘了演员:)抱歉。