2012-07-09 187 views
2

1.這是添加按鈕,當我點擊這裏我應該出現自定義對話框。在android中的自定義對話框?

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); 

我沒有得到它,一個幫助了這個錯誤

+1

嘗試使用'dialog.findViewById(...)識別textview',然後在這裏發佈你的logtrace。 – Praveenkumar 2012-07-09 05:34:29

+0

檢查此..:http://stackoverflow.com/a/10652886/1168654 – 2012-07-09 05:49:31

回答

1

應該

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

嘗試用這種line..hope它會工作。

+2

已答覆已發佈。 – user370305 2012-07-09 05:43:39

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); 
+0

謝謝你每一個人都得到它。:) – Anwesh 2012-07-09 06:09:54

0

您必須在使視圖自定義時膨脹佈局。 嘗試下面的代碼上的click事件:

  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(); 

上述聲明在code.Hope剩下的意見,這將幫助你。