2016-01-23 28 views
1

我在另一個活動中使用stablearrayadapter(完美工作)。試圖讓它在對話框片段上工作。對話框片段上的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

後膨脹R.layout.color_dialog它傳遞給listit()方法,並用它來搜索的ListView,而不是通過搜索getActivity爲ListView()。 – Luksprog

+0

那幫助,現在它不崩潰。它不顯示列表,雖然...謝謝 – Marshall

+0

您是否在查看數據庫後查看是否有一些數據? – Luksprog

回答

0

正如Luksprog指出有2個錯誤:

  1. 將listit()視圖作爲參數:

    listit(view);

,並用它爲ListView

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

... 
  • 我充氣視圖,但實際上從未將其分配給該對話框:

    視圖視圖= inflater.inflate(R.layout.color_dialog,null);

    /列表東西/

    listit();

    builder.setView(view);

    return builder.create();