0

我正在尝试使用此处的教程编写一个简单的登录页面。这是我的 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

4

3 回答 3

2

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

nameValuePairs = new ArrayList<NameValuePair>();
于 2012-09-20T09:45:12.983 回答
1

你还没有初始化nameValuePairs。

ArrayList<NameValuePair> nameValuePairs;

它具有空值。

于 2012-09-20T09:48:43.137 回答
0

-你只声明了类型的Object Reference ArrayList变量。nameValuePairsNameValuePair

-因为它是在 中声明的Class scope,并且Object它的默认值是null.

-必须初始化它......

于 2012-09-20T10:13:53.630 回答