yanivshe yanivshe - 4 months ago 66
Android Question

creating spinner inside a listView causes crashes the app

Im creating a list view where I'm inflating the rows dynamically, based on a certain variable sometimes the row is a checkbox and sometimes a spinner.

the activity can display the checkboxes no problem , but the app crashes when it tries to create the spinner .

here is my code :

DetailsAdapter(Context c,String[] titles,Map<String,List<String>> details,List<String> types)
{
super(c,R.layout.details_single_row,R.id.text_view,titles);
this.context=c;
this.titleArray=titles;
this.details=details;
this.types=types;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String mCurrentType=types.get(position);
if (mCurrentType.contains("checkbox"))
{
View row = inflater.inflate(R.layout.details_single_row, parent, false);
TextView myTitle=(TextView) row.findViewById(R.id.text_view);
ViewGroup checkBoxContainer=(ViewGroup) row.findViewById(R.id.checkBoxContainer);

myTitle.setText(titleArray[position]);
myTitle.setTextSize(40);

List<String> checkBoxTitles=details.get(titleArray[position]);

for (String value : checkBoxTitles)
{
CheckBox checkbox=new CheckBox(context);
checkbox.setText(value);
checkbox.setTextSize(40);
checkBoxContainer.addView(checkbox);
}
return row;

}else {
View row = inflater.inflate(R.layout.details_single_row_dropdown, parent, false);
TextView myTitle=(TextView) row.findViewById(R.id.textView1);
myTitle.setText(titleArray[position]);
myTitle.setTextSize(40);
List<String> spinnerTitles=details.get(titleArray[position]);
Spinner spinner = (Spinner) findViewById(R.id.spinner_details);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, spinnerTitles);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
return row;

}

}


in this line

spinner.setAdapter(spinnerArrayAdapter);


the app crashes.

stack trace :

12-24 14:31:28.362: E/AndroidRuntime(32131): FATAL EXCEPTION: main
12-24 14:31:28.362: E/AndroidRuntime(32131): Process: com.tfl.techmanager, PID: 32131
12-24 14:31:28.362: E/AndroidRuntime(32131): java.lang.NullPointerException
12-24 14:31:28.362: E/AndroidRuntime(32131): at com.tfl.techmanager.DetailsActivity$DetailsAdapter.getView(DetailsActivity.java:174)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.widget.ListView.onMeasure(ListView.java:1175)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.View.measure(View.java:16497)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.View.measure(View.java:16497)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.View.measure(View.java:16497)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-24 14:31:28.362: E/AndroidRuntime(32131): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.View.measure(View.java:16497)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-24 14:31:28.362: E/AndroidRuntime(32131): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.View.measure(View.java:16497)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.Choreographer.doFrame(Choreographer.java:544)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.os.Handler.handleCallback(Handler.java:733)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.os.Handler.dispatchMessage(Handler.java:95)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.os.Looper.loop(Looper.java:136)
12-24 14:31:28.362: E/AndroidRuntime(32131): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-24 14:31:28.362: E/AndroidRuntime(32131): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 14:31:28.362: E/AndroidRuntime(32131): at java.lang.reflect.Method.invoke(Method.java:515)
12-24 14:31:28.362: E/AndroidRuntime(32131): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-24 14:31:28.362: E/AndroidRuntime(32131): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-24 14:31:28.362: E/AndroidRuntime(32131): at dalvik.system.NativeStart.main(Native Method)


help ...please

Answer

Change this:

Spinner spinner = (Spinner) findViewById(R.id.spinner_details);

with this:

Spinner spinner = (Spinner) row.findViewById(R.id.spinner_details);

Godd luck

Comments