应用程序已停止工作的Andr​​oid [英] App has stopped working Android

查看:194
本文介绍了应用程序已停止工作的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个简单的登录页面与教程这里。这是我的Java页面:

I am trying to write a simple login page with the tutorial here. This is my Java page:

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的堆栈跟踪

And this is the 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!

我在做什么wrong.Granted我不知道多少java.BTW我的AVD是2.3.3

What am i doing wrong.Granted i don't know much java.BTW my AVD is a 2.3.3

推荐答案

在没有初始化对象 namevaluepairs中 ...

you didn't initialize the object nameValuePairs ...

nameValuePairs = new ArrayList<NameValuePair>();

这篇关于应用程序已停止工作的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆