-2

我是 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();
        }
4

2 回答 2

0

只需将您的nameField = (EditText) findViewById(R.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”获取其子项。

当你在你的activity中选择了两种不同的布局时,就意味着有两个根视图,一个被使用your Activity,可以通过ActivityName.this.findViewById()activity的方法访问。但是如果你想使用另一个布局,你需要添加另一个根视图的引用,在你的情况下textEntryView。因此,仅在该根中搜索子项。

于 2013-08-01T09:50:34.590 回答
0

假设以下观点来自R.layout.commentlayout

Dialog dialog=new Dialog(getApplicationContext);

替换以下内容,

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);
于 2013-08-01T09:50:55.107 回答