应用已停止使用Android

时间:2012-09-20 09:40:42

标签: java android xml

我正在尝试使用教程here编写一个简单的登录页面。这是我的Java页面:

public class Main extends Activity implements OnClickListener{
    EditText etUser, etPass;
    Button bLogin;

    //Create string variables that will have the input assigned to them
    String username, password;

    //Create a HTTPClient as the form container
    HttpClient httpclient;

    //Use HTTP POST method
    HttpPost httppost;

    //Create an array list for the input data to be sent
    ArrayList<NameValuePair> nameValuePairs;

    //Create a HTTP Response and HTTP Entity
    HttpResponse response;
    HttpEntity entity;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initialise();
    }

    private void initialise() {
        // TODO Auto-generated method stub
        etUser = (EditText) findViewById(R.id.username);
        etPass = (EditText) findViewById(R.id.password);
        bLogin = (Button) findViewById(R.id.login_button);
        //Now to set an onClickListener
        bLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // This is where we will be working now

        //Create new default HTTPClient
        httpclient = new DefaultHttpClient();

        //Create new HTTP POST with URL to php file as parameter
        httppost = new HttpPost("http://10.0.2.2:8080/logine/work.php");

        //Assign input text to strings
        username = etUser.getText().toString();
        password = etPass.getText().toString();

        //place them in an array list
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));

        //Next block of code needs to be surrounded by try/catch block for it to work
        try {
            //Add array list to http post
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            //assign executed form container to response
            response = httpclient.execute(httppost);

            //check status code, need to check status code 200
            if(response.getStatusLine().getStatusCode()== 200){

                //assign response entity to http entity
                entity = response.getEntity();

                //check if entity is not null
                if(entity != null){


                    //Create new input stream with received data assigned
                    InputStream instream = entity.getContent();

                    //Create new JSON Object. assign converted data as parameter.
                    JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));

                    //assign json responses to local strings
                    String retUser = jsonResponse.getString("user");//mySQL table field
                    String retPass = jsonResponse.getString("pass");

                }


            }


        } catch(Exception e){
            e.printStackTrace();
        }






    }//END onClick()

    private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }//END convertStreamToString()

}

这是Logcat Stacktrace

 java.lang.NullPointerException
        at com.Application.Main.onClick(Main.java:79)
        at android.view.View.performClick(View.java:2485)
        at android.view.View$PerformClick.run(View.java:9080)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
09-20 15:23:58.258: ERROR/InputDispatcher(60): channel '40725a60 com.Application/com.Application.Main (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
09-20 15:23:58.258: ERROR/InputDispatcher(60): channel '40725a60 com.Application/com.Application.Main (server)' ~ Channel is unrecoverably broken and will be disposed!

我做错了什么。原因我不太了解java.BTW我的AVD是2.3.3

3 个答案:

答案 0 :(得分:2)

您没有初始化对象nameValuePairs ...

nameValuePairs = new ArrayList<NameValuePair>();

答案 1 :(得分:1)

您尚未初始化nameValuePairs。

ArrayList<NameValuePair> nameValuePairs;

它具有空值。

答案 2 :(得分:0)

- 您只声明了Object Reference ArrayList类型的nameValuePairs变量NameValuePair

- 并且在Class scope及其Object中声明它的默认值为null

- 必须初始化 .....