Reputation:
Basically, I'm trying to show an AlertDialog which contains a ListView that has to get the data from an ArrayList when I press an item on mt Customized Spinner. The codes are use are below one by one in order of makeAndShowDialogBox function,setOnItemSelectedListener and 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>
Here is my Logcat below :
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)
However, as soon as I select an item on my Spinner my application crashes. Can anybody see the issue?
Upvotes: 0
Views: 262
Reputation: 44188
You need to call findViewById
on the layout you just inflated:
final View dialogView = layoutInflater.inflate(R.layout.dialog, null);
ListView listView = (ListView) dialogView.findViewById(R.id.listDialog);
Upvotes: 2