我希望这是显而易见的。我试着把一切都改回常规的"listView"(而不是使用DragNDragList库),它仍然崩溃。
我试着用一些通用的例子资源代替这些东西,但我仍然会崩溃。我觉得有一些基本的我忽略了关于ListViewCursorAdaptor,导致崩溃,我的问题不是专属于dragNdrop库。
问题是否与它在片段中有关?
我onCreateView:
myFragmentView = inflater.inflate(R.layout.todo_fragment, container, false);
db = new DBAdapter(getActivity());
db.open();
Cursor TaskCursor = db.getAllTasks();
list = (DragNDropListView) getActivity().findViewById(R.id.dragList);
Log.d("cursor", TaskCursor.getString(1)); //This shows me that my cursor obviously isn't empty
adapter = new DragNDropCursorAdapter(myFragmentView.getContext(),
R.layout.task_list_item,
TaskCursor,
new String[]{DBAdapter.COLUMN_NAME},
new int[]{R.id.TaskItemTitle},
R.id.DragHandle);
Log.d("apater", "test");
list.setDragNDropAdapter(adapter); //Line 46 (where it crashes)
Log.d("apater", "test");
db.close();
return myFragmentView;
My list fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.terlici.dragndroplist.DragNDropListView
android:id="@+id/dragList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/></LinearLayout>
My List Item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/DragHandle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/drag_handle" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TaskItemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/card_text" >
</RelativeLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textSize="16sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Small Text"
android:textSize="8sp" />
</LinearLayout>
图书馆:https://github.com/terlici/DragNDropList
和LogCa:
05-05 12:16:19.456: E/AndroidRuntime(29575): FATAL EXCEPTION: main
05-05 12:16:19.456: E/AndroidRuntime(29575): Process: edu.jcu.cs470.togenda, PID: 29575
05-05 12:16:19.456: E/AndroidRuntime(29575): java.lang.NullPointerException
05-05 12:16:19.456: E/AndroidRuntime(29575): at edu.jcu.cs470.togenda.ToDoFragment.onCreateView(ToDoFragment.java:46)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.app.Fragment.performCreateView(Fragment.java:1700)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.os.Handler.handleCallback(Handler.java:733)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.os.Handler.dispatchMessage(Handler.java:95)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.os.Looper.loop(Looper.java:136)
05-05 12:16:19.456: E/AndroidRuntime(29575): at android.app.ActivityThread.main(ActivityThread.java:5024)
05-05 12:16:19.456: E/AndroidRuntime(29575): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 12:16:19.456: E/AndroidRuntime(29575): at java.lang.reflect.Method.invoke(Method.java:515)
05-05 12:16:19.456: E/AndroidRuntime(29575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-05 12:16:19.456: E/AndroidRuntime(29575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-05 12:16:19.456: E/AndroidRuntime(29575): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
05-05 12:16:19.456: E/AndroidRuntime(29575): at dalvik.system.NativeStart.main(Native Method)
05-05 12:17:01.898: I/Process(29575): Sending signal. PID: 29575 SIG: 9
你的日志说这是一个null点异常,它在listview中。请尝试替换
list = (DragNDropListView) getActivity().findViewById(R.id.dragList);
list = (DragNDropListView) myFragmentView.findViewById(R.id.dragList);
您正在使用getactivity()请使用您的充气视图名称,在您的情况下,它是myFragmentView,为什么一个零点异常正在发生
您需要将下面的代码移动到onViewCreated()。我的猜测是findViewById返回null,因为视图仍在创建中。
db = new DBAdapter(getActivity());
db.open();
Cursor TaskCursor = db.getAllTasks();
list = (DragNDropListView) getActivity().findViewById(R.id.dragList);
Log.d("cursor", TaskCursor.getString(1)); //This shows me that my cursor obviously isn't empty
adapter = new DragNDropCursorAdapter(myFragmentView.getContext(),
R.layout.task_list_item,
TaskCursor,
new String[]{DBAdapter.COLUMN_NAME},
new int[]{R.id.TaskItemTitle},
R.id.DragHandle);
Log.d("apater", "test");
list.setDragNDropAdapter(adapter); //Line 46 (where it crashes)
Log.d("apater", "test");
db.close();
和替换
list = (DragNDropListView) getActivity().findViewById(R.id.dragList);
list = (DragNDropListView) view.findViewById(R.id.dragList);