0

ダイアログボックスを使用してデータベースに値を追加しようとしています。ただし、値は常にnullになるため、レイアウトから値を取得できないようです。これが私のコードです:

public class WlAddDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setView(inflater.inflate(com.mutoor.autoreply.R.layout.whitelistadd, null));
    builder.setPositiveButton(com.mutoor.autoreply.R.string.add, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Log.d("Dialog", "Starting dialog");
            Context c = getActivity().getApplicationContext();
            Log.d("Dialog", "Context Received");
            WlHelper wl = new WlHelper(c);
            Log.d("Dialog", "Created Whitelist Helper");
            TextView eName = (TextView) getActivity().findViewById(R.id.entryName);
            TextView eNumber = (TextView) getActivity().findViewById(R.id.entryNumber);
            Log.d("Dialog", "Name = " + eName.getText().toString() + " , Number = " + eNumber.getText().toString());
            wl.createEntry(eName.getText().toString(), eNumber.getText().toString());
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(com.mutoor.autoreply.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    return builder.create();
}

そして、これが私の出力です:

01-02 22:23:23.350: D/Dialog(26471): Starting dialog
01-02 22:23:23.350: D/Dialog(26471): Context Received
01-02 22:23:23.358: D/Dialog(26471): Created Whitelist Helper
01-02 22:23:23.358: D/AndroidRuntime(26471): Shutting down VM
01-02 22:23:23.358: E/AndroidRuntime(26471): FATAL EXCEPTION: main
01-02 22:12:57.373: E/AndroidRuntime(25973): java.lang.NullPointerException
01-02 22:23:23.358: E/AndroidRuntime(26471): at com.mutoor.autoreply.WlAddDialog$1.onClick(WlAddDialog.java:36)

どんな助けでも大歓迎です。

4

4 に答える 4

3

使用する

TextView eName = (TextView)view.findViewById(R.id.entryName);
TextView eNumber = (TextView)view.findViewById(R.id.entryNumber);

ダイアログのカスタム レイアウトを使用している場合は、TextView 値を取得します。

編集: コードを次のように変更します。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

     View view = inflater.inflate(com.mutoor.autoreply.R.layout.whitelistadd, null);
    builder.setView(view);
            TextView eName = (TextView)view.findViewById(R.id.entryName);
            TextView eNumber = (TextView)view.findViewById(R.id.entryNumber);

    builder.setPositiveButton(com.mutoor.autoreply.R.string.add, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Log.d("Dialog", "Starting dialog");
            Context c = getActivity().getApplicationContext();
            Log.d("Dialog", "Context Received");
            WlHelper wl = new WlHelper(c);
            Log.d("Dialog", "Created Whitelist Helper");

            Log.d("Dialog", "Name = " + eName.getText().toString() + " , Number = " + eNumber.getText().toString());
            wl.createEntry(eName.getText().toString(), eNumber.getText().toString());
            dialog.dismiss();
        }
    });
于 2013-01-03T06:34:32.247 に答える
1

両方の TextView の場合

TextView eName = (TextView) getActivity().findViewById(R.id.entryName);
TextView eNumber = (TextView) getActivity().findViewById(R.id.entryNumber);

ダイアログレイアウトの一部である場合、そうあるべきです。

TextView eName = (TextView) dialog.findViewById(R.id.entryName);
TextView eNumber = (TextView) dialog.findViewById(R.id.entryNumber);
于 2013-01-03T06:34:16.207 に答える
0

ダイアログのレイアウトではなく、アクティビティのレイアウトからテキスト ボックスを検索しているようです。ダイアログでTextViewを検索し、そこに提供された値を使用してデータベーストランザクションを完了することを期待しています。

于 2013-01-03T06:33:53.163 に答える
0

あなたはこのように:

TextView eName = (TextView)layout.findViewById(R.id.entryName);
TextView eNumber = (TextView)layout.findViewById(R.id.entryNumber);

ダイアログレイアウトからビューにアクセスするために定義する必要があるのは、layout.findViewById()それがnullpointerエラーが発生する理由だけです。試してみてください。これが唯一の問題だと思います。

于 2013-01-03T06:40:00.090 に答える