Java NullPointerException - 从文本框设置字符串参数

时间:2014-07-09 06:08:59

标签: java android android-volley

我试图在我的Volley请求中设置一个参数,并且我没有正确地从输入字段(在对话框中)获取和设置参数 - 导致空指针。服务器端确认参数为空。这是我的listDialog方法:

public void listDialog() {
    // get layout for prompt
    LayoutInflater layoutInflater = LayoutInflater.from(this);

View promptView = layoutInflater.inflate(R.layout.list_name_prompt,  null);

final EditText input = (EditText) promptView.findViewById(R.id.userInput);

listName = (input.getText().toString());

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

//set promptview to be the layout file for alerdialog builder
alertDialogBuilder.setView(promptView);

// setup a dialog window
alertDialogBuilder
    .setCancelable(false)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        EditText listNameBox = (EditText) findViewById(R.id.userInput);
        listName = listNameBox.getText().toString();

            createListRequest();
        }
    })

    .setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    AlertDialog alertD = alertDialogBuilder.create();

    alertD.show();
}

我将listName设置在我的活动顶部:

private String listName;

createListRequest方法中,我使用参数如下:

public void createListRequest(){

JsonObjectRequest createRequest = new JsonObjectRequest(Request.Method.POST, createUrl, null,
    new Response.Listener<JSONObject>() {
        @Override public void onResponse(JSONObject response) {
            try {
                //success
                if (response.getBoolean("success")) {
                    Log.d("Response", response.toString());
                } 
            } catch (Exception e) {
                //not success
                Log.d("Error.Response", e.toString());
            }
        }
    },
    new Response.ErrorListener() {
        @Override public void onErrorResponse(VolleyError error) {
            Log.d("Error.response", error.toString());
        }
    }) {
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type",  "application/json");
        headers.put("auth_token", mPreferences.getString("AuthToken", "") );
        return headers;
    }

    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("name", listName);

        return params;
    }
};
AppController.getInstance().addToRequestQueue(createRequest);
}

我是第一个承认我不了解Java变量和类字段以及我应该理解的人 - 这可能就是一个症状。

3 个答案:

答案 0 :(得分:1)

您的listNameBox为空,因为您的对话框视图为promptViewuserInput ID属于promptView,因此请更改

    EditText listNameBox = (EditText) findViewById(R.id.userInput);

    EditText listNameBox = (EditText)promptView.findViewById(R.id.userInput);

答案 1 :(得分:1)

在第22行,您忘记了 findViewById 的班级名称。像第7行:

promptView.findViewById(R.id.userInput);

答案 2 :(得分:0)

问题是你正在使用findViewbyId而不是使用alertD.findViewById然后它会工作

setPositiveButton(&#34; OK&#34;,new DialogInterface.OnClickListener(){         public void onClick(DialogInterface对话框,int id){

    EditText listNameBox = (EditText) alertD.findViewById(R.id.userInput);
    listName = listNameBox.getText().toString();

        createListRequest();
    }
})