0

チュートリアル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 スタックトレースです

 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;

そしてそれはnull値を持っています。

于 2012-09-20T09:48:43.137 に答える
0

- typeのObject Reference ArrayList変数のみを宣言しました。nameValuePairsNameValuePair

-で宣言されてClass scopeいるObjectように、デフォルト値はnullです。

-初期化する必要があります.....

于 2012-09-20T10:13:53.630 に答える