我希望它有点明显。我尝试将所有内容更改回常规“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;
我的列表片段:
<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>
我的清单项目:
<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
答案 0 :(得分:2)
你记录说它是一个零点异常,它在listview.please中尝试替换
list = (DragNDropListView) getActivity().findViewById(R.id.dragList);
到
list = (DragNDropListView) myFragmentView.findViewById(R.id.dragList);
您正在使用 getactivity()请在您的情况下使用您的膨胀视图名称 myFragmentView ,了解为什么会出现null point excetin。
答案 1 :(得分:1)
您需要将代码移到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);