Android - 回调以在Spinner选择中显示自定义AlertDialog?

时间:2014-11-16 11:49:24

标签: android

基本上,我正在尝试显示一个AlertDialog,其中包含一个ListView,当我按下mt Customized Spinner上的项目时,它必须从ArrayList获取数据。按照 makeAndShowDialogBox 函数, setOnItemSelectedListener dialog.xml 的顺序逐个使用代码:

makeAndShowDialogBox:

private void makeAndShowDialogBox(){

AlertDialog.Builder myDialogBox = new AlertDialog.Builder(this);

final LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

final View dialogView = layoutInflater.inflate(R.layout.dialog, null);

ListView listView = (ListView) findViewById(R.id.listDialog);

ListAdapter adapter = new ArrayAdapter<String>(ActivityWithCustomizedSpinner.this,android.R.layout.simple_list_item_1, carNames);

listView.setAdapter(adapter);

// Set three option buttons
myDialogBox.setPositiveButton("Close",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // whatever should be done when answering "YES" goes
                // here

            }
        });

myDialogBox.setView(dialogView);
myDialogBox.show();

}

setOnItemSelectedListener

spin.setAdapter(new MySpinnerAdapter(this, R.layout.spinner_layout,
            SpinnerValues));

spin.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
            if (isDefaultSelection) {
            isDefaultSelection = false;
            }else{

            carNames = brand.getChildCarNames(brand.getListDataHeader().get(position));
            makeAndShowDialogBox();
            }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub

    }

});

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@string/activity3_name" >

    <ListView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listDialog">
    </ListView>

</LinearLayout>

以下是我的Logcat:

11-16 14:16:34.602: W/dalvikvm(18958): threadid=1: thread exiting with uncaught exception (group=0x41ce8700)
11-16 14:16:34.622: E/AndroidRuntime(18958): FATAL EXCEPTION: main
11-16 14:16:34.622: E/AndroidRuntime(18958): java.lang.NullPointerException
11-16 14:16:34.622: E/AndroidRuntime(18958):    at com.renc.saridogan.hw2.ActivityWithCustomizedSpinner.makeAndShowDialogBox(ActivityWithCustomizedSpinner.java:79)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at com.renc.saridogan.hw2.ActivityWithCustomizedSpinner.access$4(ActivityWithCustomizedSpinner.java:67)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at com.renc.saridogan.hw2.ActivityWithCustomizedSpinner$1.onItemSelected(ActivityWithCustomizedSpinner.java:54)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:899)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at android.widget.AdapterView.access$200(AdapterView.java:50)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:863)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at android.os.Handler.handleCallback(Handler.java:730)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at android.os.Looper.loop(Looper.java:176)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at android.app.ActivityThread.main(ActivityThread.java:5419)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at java.lang.reflect.Method.invoke(Method.java:525)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
11-16 14:16:34.622: E/AndroidRuntime(18958):    at dalvik.system.NativeStart.main(Native Method)

然而,只要我在Spinner上选择一个项目,我的应用程序就崩溃了。任何人都可以看到这个问题吗?

1 个答案:

答案 0 :(得分:2)

您需要在刚刚夸大的布局上调用findViewById

final View dialogView = layoutInflater.inflate(R.layout.dialog, null);
ListView listView = (ListView) dialogView.findViewById(R.id.listDialog);