-2

私はアンドロイドが初めてで、これが私の質問です。フォームの送信ボタンをクリックすると、フォームの送信ボタンをクリックすると、アプリが強制的に閉じられます。ヌル ポインターであるという 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」に基づいて子を取得するために使用されます。

アクティビティで 2 つの異なるレイアウトを選択すると、ルート ビューが 2 つあり、そのうちの 1 つが使用され、アクティビティのメソッドyour Activityからアクセスできます。ActivityName.this.findViewById()ただし、別のレイアウトを使用する場合は、別のルート ビューの参照を追加する必要があります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 に答える