在向我的片段添加微调器时总是会出现NullPointerException

时间:2015-11-27 00:21:51

标签: android xml android-layout android-fragments android-spinner

我尝试使用微调器创建联系人列表,您可以在其中为联系人选择不同的角色。

我总是在将微调器添加到我的联系人列表(list_item.xml)的布局时得到NullPointerException,而我却无法找到我的错误。

kontakte_fragment - XML文件

<?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>

我的列表布局(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

2 个答案:

答案 0 :(得分:0)

您正在片段的布局中寻找Spinner,但它位于LitsView的元素布局中。 您可以在适配器的getView方法中获得对Spinner的正确引用,如下所示:

Spinner yourSpinner = (Spinner) convertView.findViewById(R.id.spinnerKontakte);

答案 1 :(得分:0)

你在哪里添加Spinner片段?你是以错误的方式做到这一点

public class KontakteFragment extends Fragment {

private ArrayList<String> data = new ArrayList<String>();

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));


    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);
            viewHolder.spinner = (Spinner)convertView.findViewById(R.id.spinnerKontakte);
        }

        else{
            mainViewHolder = (ViewHolder) convertView.getTag();
            mainViewHolder.titel.setText(getItem(position));
            mainViewHolder.unterTitel.setText(getItem(position));
          // here you have to set data to spinner 
        }

        return convertView;
    }
}

public class ViewHolder {

    ImageView thumbnail;
    TextView titel;
    TextView unterTitel;
    Spinner spinner
}

  }