I'm trying to create a contact list with a spinner where you can choose different roles for contacts.
I'm always getting a NullPointerException on adding the spinner to the layout of my contact list (list_item.xml) and I just can't find my mistake(s).
kontakte_fragment - XML File
<?xml version="1.0" encoding="utf-8"?> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1" android:background="#ffffff"> <ListView android:layout_width="fill_parent" android:layout_height="657dp" android:id="@+id/listViewKontakte" /> </LinearLayout> </ScrollView>
The Layout for my List (list_item.xml)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:background="#ffffff"> <com.github.siyamed.shapeimageview.CircularImageView android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/screen1" app:siBorderWidth="2dp" app:siBorderColor="@color/colorPrimary" android:layout_gravity="center" android:id="@+id/kontaktListePic" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/titelKontaktListe" android:id="@+id/textview1kontakteTitel" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:layout_marginTop="15dp" android:layout_alignTop="@+id/kontaktListePic" android:layout_toRightOf="@+id/kontaktListePic" android:layout_toEndOf="@+id/kontaktListePic" android:layout_marginBottom="0dp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/unterTitelKontaktListe" android:id="@+id/untertitelKontaktListe" android:textSize="12sp" android:layout_below="@+id/textview1kontakteTitel" android:layout_alignLeft="@+id/textview1kontakteTitel" android:layout_alignStart="@+id/textview1kontakteTitel" /> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinnerKontakte" android:layout_alignBottom="@+id/untertitelKontaktListe" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginLeft="250dp" android:layout_marginTop="15dp" android:layout_marginBottom="-5dp" /> </RelativeLayout>
KontakteFragment - ActivityClass
public class KontakteFragment extends Fragment { private ArrayList<String> data = new ArrayList<String>(); Spinner spinner; ArrayAdapter<CharSequence> adapter; public static KontakteFragment newInstance() { KontakteFragment fragment = new KontakteFragment(); return fragment; } public KontakteFragment(){ } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.kontakte_fragment, container, false); ListView lv = (ListView) rootView.findViewById(R.id.listViewKontakte); generateListContent(); lv.setAdapter(new MyListAdapter(getContext(), R.layout.list_item, data)); spinner = (Spinner) rootView.findViewById(R.id.spinnerKontakte); adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.profile_kontakte, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); return rootView; } private void generateListContent() { for(int i = 0; i < 55; i++){ data.add("This is row number" + i); } } private class MyListAdapter extends ArrayAdapter<String>{ private int layout; public MyListAdapter(Context context, int resource, List<String> objects) { super(context, resource, objects); layout = resource; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder mainViewHolder = null; if(convertView == null){ LayoutInflater inflater = LayoutInflater.from(getContext()); convertView = inflater.inflate(layout, parent, false); ViewHolder viewHolder = new ViewHolder(); viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.kontaktListePic); viewHolder.titel = (TextView) convertView.findViewById(R.id.textview1kontakteTitel); viewHolder.unterTitel = (TextView) convertView.findViewById(R.id.untertitelKontaktListe); convertView.setTag(viewHolder); } else{ mainViewHolder = (ViewHolder) convertView.getTag(); mainViewHolder.titel.setText(getItem(position)); mainViewHolder.unterTitel.setText(getItem(position)); } return convertView; } } public class ViewHolder { ImageView thumbnail; TextView titel; TextView unterTitel; } } // This is the end of our KontakteFragment Class
No comments:
Post a Comment