我正在尝试阅读一本书,但我错过了本教程中的一些内容。但是我在某个地方迷路了,我试图弄清楚为什么会因空指针异常而崩溃。只有一个人能看到我在这里缺少什么吗?
public class HistoryFragment extends Fragment {
DbHelper dbHelper;
SQLiteDatabase db;
Cursor cursor;
ListView listTimeline;
SimpleCursorAdapter adapter;
static final String[] FROM = {DbHelper.KEY_CHARTING_DATE, DbHelper.KEY_NAME, DbHelper.KEY_CHARTING_NOTES};
static final int[] TO = {R.id.textDate, R.id.textUser, R.id.textText};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
listTimeline = (ListView) getView().findViewById(R.id.listTimeline);
//connect to database
dbHelper = new DbHelper(getActivity());
db = dbHelper.getReadableDatabase();
return view;
}
@Override
public void onDestroy(){
super.onDestroy();
//close datbase
db.close();
}
@Override
public void onResume(){
super.onResume();
//Get the data from the database
cursor = db.query(DbHelper.DATABASE_TABLE, null, null, null, null, null, DbHelper.KEY_CHARTING_DATE + " DESC");
getActivity().startManagingCursor(cursor);
//Sup up the adapter
adapter.setViewBinder(VIEW_BINDER);
listTimeline.setAdapter(adapter);
}
static final ViewBinder VIEW_BINDER = new ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() != R.id.textDate)
return false;
//update the created at text
long timestamp = cursor.getLong(columnIndex);
CharSequence relTime = DateUtils.getRelativeTimeSpanString(view.getContext(), timestamp);
((TextView) view).setText(relTime);
return true;
}
};
}
日志猫:
05-15 20:37:49.412: E/AndroidRuntime(945): java.lang.NullPointerException
05-15 20:37:49.412: E/AndroidRuntime(945): at com.projectcaruso.naturalfamilyplaning.HistoryFragment.onCreateView(HistoryFragment.java:41)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.os.Handler.handleCallback(Handler.java:725)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.os.Looper.loop(Looper.java:137)
05-15 20:37:49.412: E/AndroidRuntime(945): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-15 20:37:49.412: E/AndroidRuntime(945): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 20:37:49.412: E/AndroidRuntime(945): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 20:37:49.412: E/AndroidRuntime(945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-15 20:37:49.412: E/AndroidRuntime(945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-15 20:37:49.412: E/AndroidRuntime(945): at dalvik.system.NativeStart.main(Native Method)