2

1.这是添加按钮,当我点击这里时,我应该会出现自定义对话框。

Button add = (Button) findViewById(R.id.addbutton);
// add button listener
add.setOnClickListener(new OnClickListener() {

2.这是自定义对话框代码

    // custom dialog
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.form);
    dialog.setTitle("Custom dailog");

    TextView uname_TV = (TextView) findViewById(R.id.Uname_TV);
    EditText uname_ET = (EditText) findViewById(R.id.Uname_ET);
    TextView dname_TV = (TextView) findViewById(R.id.Drugname_TV);
    EditText dname_ET = (EditText) findViewById(R.id.Drugname_ET);
    spinner = (Spinner) findViewById(R.id.spinner_id);

    ArrayAdapter < String > adapter = new ArrayAdapter < String > (
    context, android.R.layout.simple_spinner_item,
    noof_times);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setSelection(1);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView <? > parent,
        View view, int position, long id) {
            String item = parent.getItemAtPosition(position).toString();
            String item1 = parent.getSelectedItem().toString();
            Toast.makeText(getApplicationContext(), "Selected:" + item, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView <? > arg0) {
            // TODO Auto-generated method stub
        }
    });
    dialog.show();

我正进入(状态

  Null pointer exception at spinner.setAdapter(adapter);

我没明白,这个错误的一个帮助

4

3 回答 3

3

是的,你会得到,因为

TextView uname_TV = (TextView) findViewById(R.id.Uname_TV);
EditText uname_ET = (EditText) findViewById(R.id.Uname_ET);
TextView dname_TV = (TextView) findViewById(R.id.Drugname_TV);
EditText dname_ET = (EditText) findViewById(R.id.Drugname_ET);
spinner = (Spinner) findViewById(R.id.spinner_id);

将这些更改为

TextView uname_TV = (TextView) dialog.findViewById(R.id.Uname_TV);
EditText uname_ET = (EditText) dialog.findViewById(R.id.Uname_ET);
TextView dname_TV = (TextView) dialog.findViewById(R.id.Drugname_TV);
EditText dname_ET = (EditText) dialog.findViewById(R.id.Drugname_ET);
spinner = (Spinner) dialog.findViewById(R.id.spinner_id);
于 2012-07-09T05:35:38.587 回答
1

它应该是

spinner = (Spinner) dialog.findViewById(R.id.spinner_id);

试试这条线..希望它会起作用。

于 2012-07-09T05:39:31.497 回答
0

您必须在自定义视图时扩展布局。在点击事件上尝试下面的代码:

            LayoutInflater inflater=LayoutInflater.from(YouActivityName.this);
            View view=inflater.inflate(R.layout.form, null);
            AlertDialog.Builder builder=new AlertDialog.Builder(YouActivityName.this);
            builder.setView(view);
            Spinner spinner=(Spinner) view.findViewById(R.id.spinner_id);
            ArrayAdapter<String > adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,item);
            spinner.setAdapter(adapter);
            builder.create();
            builder.show();

在代码中声明上面的其余视图。希望这会对您有所帮助。

于 2012-07-09T05:53:49.567 回答