对话框中的微调器:NullPointerException

时间:2017-04-26 11:54:49

标签: android nullpointerexception android-spinner

美好的一天我在对话框中使用微调器并生成

CMAKE_*_LINKER_FLAGS

Java代码

Error msg- ` java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference at com.example.amw51.ygeianapp.Activity.AwardActivity.showAddItem(AwardActivity.java:259)

XML布局

 dialog.setContentView(R.layout.job_add_detials_layout);
Spinner   jobSpinner = (Spinner)findViewById(R.id.select_job_spinner);
 jobSpinner = (Spinner) findViewById(R.id.select_job_spinner);
                List<String> list = new ArrayList<String>();
                list.add("Android");
                list.add("Java");
                list.add("Spinner Data");
                list.add("Spinner Adapter");
                list.add("Spinner Example");

                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                        (this, android.R.layout.simple_spinner_item,list);

                dataAdapter.setDropDownViewResource
                        (android.R.layout.simple_spinner_dropdown_item);

                jobSpinner.setAdapter(dataAdapter);
 dialog.show();              

我到处搜索但没找到任何合适的解决方案。                             `

1 个答案:

答案 0 :(得分:3)

从对话框本身检索Spinner。您应该执行dialog.findViewById()而不是普通findViewById()

dialog.setContentView(R.layout.job_add_detials_layout);
dialog.create();
Spinner jobSpinner = (Spinner) dialog.findViewById(R.id.select_job_spinner);
// init spinner
dialog.show();