【问题标题】:App has stopped working Android应用程序已停止运行 Android
【发布时间】:2012-09-12 15:50:51
【问题描述】:

我正在尝试使用教程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

【问题讨论】:

  • com.Application.Main.onClick(Main.java:79) 行是什么??
  • 它的nameValuePairs.add(new BasicNameValuePair("username", username)); nameValuePairs.add(new BasicNameValuePair("password", password));

标签: java android xml


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    nameValuePairs = new ArrayList<NameValuePair>();
    

    【讨论】:

    • 我已经初始化了,一开始我必须再做一次。如果我必须把它放在哪里?它正在获取 etuser 和 et 密码,但它没有传递那些进入用户名。
    • 你没有类似 nameValuePairs = new ArrayList();
    • 我的用户名还是空白。我该怎么办?
    【解决方案3】:

    您尚未初始化 nameValuePairs。

    ArrayList<NameValuePair> nameValuePairs;
    

    而且它有空值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 2016-01-29
      • 2018-09-25
      相关资源
      最近更新 更多