Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图将对话框表单发布到http URL时显示空指针的Android对话框_Android_Http_Dialog - Fatal编程技术网

试图将对话框表单发布到http URL时显示空指针的Android对话框

试图将对话框表单发布到http URL时显示空指针的Android对话框,android,http,dialog,Android,Http,Dialog,我是android新手,这是我的问题,我有一个弹出的对话框表单,它假设使用android Http post将表单提交到URL,当我单击表单的提交按钮时,它强制关闭应用程序,我从logcat收到一条错误消息,说它是空指针 protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_TEXT_ENTRY: //This shows how to add a custom la

我是android新手,这是我的问题,我有一个弹出的对话框表单,它假设使用android Http post将表单提交到URL,当我单击表单的提交按钮时,它强制关闭应用程序,我从logcat收到一条错误消息,说它是空指针

protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_TEXT_ENTRY:
            //This shows how to add a custom layout to an AlertDialog 
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.commentlayout, null);
            return new AlertDialog.Builder(HomeActivity.this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle(R.string.app_name)
                .setView(textEntryView)
                .setPositiveButton(R.string.Submit, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    postComment();

                }
            })
                .setNegativeButton(R.string.cancal, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    /* User clicked cancel so do some stuff */
                }
            }).create();
    }
    return null;
}

//this comes after the setContentView(R.Layout.view)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);

    public void postComment() {

        nameField = (EditText) findViewById(R.id.editText1);
        countryField = (EditText) findViewById(R.id.editText2);
        commentField = (EditText) findViewById(R.id.commentField);

        //get message from message fields
        String name = nameField.getText().toString();
        String count = countryField.getText().toString();
        String comm = commentField.getText().toString();

        //check whether the name field is empty or not
        if (name.length() > 0) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://_______");

            try {
                List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > (3);

                nameValuePairs.add(new BasicNameValuePair("namet", name));
                nameValuePairs.add(new BasicNameValuePair("countryt", count));
                nameValuePairs.add(new BasicNameValuePair("commentt", comm));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);

                nameField.setText(""); //reset the message text field
                countryField.setText("");
                commentField.setText("");

                Toast.makeText(getBaseContext(), "Sent", Toast.LENGTH_SHORT).show();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            //display message if text field is empty
            Toast.makeText(getBaseContext(), "All fields are required", Toast.LENGTH_SHORT).show();
        }
只需替换nameField=EditText findViewByIdR.id.editText1;和其他2个具有以下代码

nameField = (EditText) textEntryView.findViewById(R.id.editText1);
countryField = (EditText) textEntryView.findViewById(R.id.editText2);
commentField = (EditText) textEntryView.findViewById(R.id.commentField);
编辑

为什么要添加textEntryView

答案是,当您使用任何布局时,它都有根视图。该根视图用于根据子视图的资源ID提取其子视图


当您在活动中选择两个不同的布局时,这意味着有两个根视图,其中一个由活动使用,可通过ActivityName.this.findViewById方法访问。但如果您想使用另一个布局,则需要添加另一个根视图的引用,该根视图位于您的案例textEntryView中。因此,只在该根中搜索儿童

假设以下视图来自R.layout.commentlayout

替换以下内容:

nameField = (EditText) findViewById(R.id.editText1);
countryField = (EditText) findViewById(R.id.editText2);
commentField = (EditText) findViewById(R.id.commentField);


你能从LogCat发布错误的stacktrace吗?你能告诉我在哪一个地方得到空指针异常吗?谢谢你们的回复,我在这里得到它,String name=nameField.getText.toString;nameField初始化了吗?如果你们有一个解决方案或者更好的方法,请回答,如果有效的话,我很乐意接受你们的回答。你可以编辑我发布的代码。我现在会尝试这种方法,并立即回复你。我会尝试这种方法,并回复你。谢谢你的回答。成功了。我刚试过。但是,请您解释一下为什么要将textEntryView添加到它中,这样可以帮助以后可能会遇到类似问题的其他人
nameField = (EditText) findViewById(R.id.editText1);
countryField = (EditText) findViewById(R.id.editText2);
commentField = (EditText) findViewById(R.id.commentField);
nameField = (EditText)dialog.findViewById(R.id.editText1);
countryField = (EditText)dialog.findViewById(R.id.editText2);
commentField = (EditText)dialog.findViewById(R.id.commentField);