对话框片段上的Stablearrayadapter上下文

时间:2016-01-23 09:44:19

标签: android android-fragments dialog android-arrayadapter

我正在另一项活动中使用stablearrayadapter(效果很好)。试图让它在一个对话框片段上工作。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: eu.lunca.santa, PID: 22179
        java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at eu.lunca.santa.SearchDialog.listit(SearchDialog.java:98)
        at eu.lunca.santa.SearchDialog.onCreateDialog(SearchDialog.java:62)
        at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:308)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5389)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

现在根据我的理解,问题是setadapter的参数中有一些空对象。

private void listit(){
    final ListView listview = (ListView) getActivity().findViewById(R.id.listBooks);



    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < ll.size(); ++i) {
        list.add(ll.get(i));
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, list); //A: Think problem is here

    listview.setAdapter(adapter); //B: Where it crashes

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, final View view,
                                int position, long id) {
            Log.v(TAG, "Touch: " + position);
            switch (position){
                //do something
            }
        }
    });
}

现在我尝试在stablearraadapter中打印所有参数,但没有一个是空的。如果问题是第一个元素(上下文),我已经尝试了很多东西(getactivity(),getcontext(),getactivity()。getbasecontext()等。)

任何可以帮我弄清问题是什么的人?

整个对话框片段(以防):

public class SearchDialog extends DialogFragment {

private DBAdapter db;
private LinkedList<String> ll;
private Singleton s;

//log
final String TAG = "TColorDialogFragment";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    s = (Singleton) getActivity().getApplication();
    ll = new LinkedList<String>();
    db = new DBAdapter(getContext());
    cargarLibros();

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setTitle(R.string.choosebook);
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            SearchDialog.this.getDialog().cancel();
        }
    });

    View view = inflater.inflate(R.layout.color_dialog, null);

    /*List stuff*/
    listit();

    return builder.create();
}

private void cargarLibros() {
    db.open();
    Cursor libros = db.getLibros();
    db.close();
    do {
        ll.add(libros.getString(1));
    } while (libros.moveToNext());
    //setListAdapter(new LibroAdapter(this, R.layout., ll)); //fix
}

private void listit(){
    final ListView listview = (ListView) getActivity().findViewById(R.id.listBooks);

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < ll.size(); ++i) {
        list.add(ll.get(i));
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, final View view,
                                int position, long id) {
            Log.v(TAG, "Touch: " + position);
            switch (position){
                //do something
            }
        }
    });
}

private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
                              List<String> objects) {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
}

}

编辑:color_dialog.xml:

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

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listBooks"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

正如Luksprog指出的那样,有两个错误:

  1. 将listit()视图作为参数:

    listit(视图);

  2. 并将其用于listview

    private void listit(View view){
    final ListView listview = (ListView) view.findViewById(R.id.listBooks);
    
    ...
    
    1. 我给视图充气但从未实际将其分配给对话框:

      View view = inflater.inflate(R.layout.color_dialog,null);

      / 列出内容 /

      listit();

      builder.setView(视图);

      返回builder.create();