我正在尝试从我的数据库中获取信息列表并将其显示到自定义列表视图中。我已经有了DatabaseHelper,Fragment和Adapter设置。以下是我收到的错误:
04-29 13:36:52.539: E/AndroidRuntime(8135): FATAL EXCEPTION: main
04-29 13:36:52.539: E/AndroidRuntime(8135): java.lang.NullPointerException
04-29 13:36:52.539: E/AndroidRuntime(8135): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.<init>(SQLiteAssetHelper.java:110)
04-29 13:36:52.539: E/AndroidRuntime(8135): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.<init>(SQLiteAssetHelper.java:130)
04-29 13:36:52.539: E/AndroidRuntime(8135): at com.rcd.mypr.Workouts.WorkoutsDatabaseHelper.<init>(WorkoutsDatabaseHelper.java:35)
04-29 13:36:52.539: E/AndroidRuntime(8135): at com.rcd.mypr.Workouts.WorkoutsFragment.onCreateView(WorkoutsFragment.java:36)
04-29 13:36:52.539: E/AndroidRuntime(8135): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
04-29 13:36:52.539: E/AndroidRuntime(8135): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
04-29 13:36:52.539: E/AndroidRuntime(8135): at android.app.BackStackRecord.run(BackStackRecord.java:622)
04-29 13:36:52.539: E/AndroidRuntime(8135): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
04-29 13:36:52.539: E/AndroidRuntime(8135): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
04-29 13:36:52.539: E/AndroidRuntime(8135): at android.os.Handler.handleCallback(Handler.java:605)
04-29 13:36:52.539: E/AndroidRuntime(8135): at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 13:36:52.539: E/AndroidRuntime(8135): at android.os.Looper.loop(Looper.java:137)
04-29 13:36:52.539: E/AndroidRuntime(8135): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-29 13:36:52.539: E/AndroidRuntime(8135): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 13:36:52.539: E/AndroidRuntime(8135): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 13:36:52.539: E/AndroidRuntime(8135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-29 13:36:52.539: E/AndroidRuntime(8135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-29 13:36:52.539: E/AndroidRuntime(8135): at dalvik.system.NativeStart.main(Native Method)
这是我的WorkoutsDatabaseHelper.java
package com.rcd.mypr.Workouts;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class WorkoutsDatabaseHelper extends SQLiteAssetHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "workoutsDatabase.sqlite";
private static final String WORKOUTS_TABLE = "workouts";
// Workouts Table Columns names
private static final String KEY_ID = "_id";
private static final String KEY_WORKOUT_NAME = "workout_name";
private static final String KEY_HERO_LAST_NAME = "hero_last_name";
private static final String KEY_HERO_FIRST_NAME = "hero_first_name";
private static final String KEY_HERO_MIDDLE_NAME = "hero_middle_name";
private static final String KEY_HERO_DESCRIPTION = "hero_description";
private static final String KEY_DATE_ADDED = "date_added";
private static final String KEY_WORKOUT = "workout";
private static final String KEY_MEASUREMENT = "measurement";
private static final String KEY_PHOTO = "photo";
public WorkoutsDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public Cursor getWorkoutNames() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(WORKOUTS_TABLE, new String[] {"_id", "workout_name"}, null, null, null,
null, KEY_WORKOUT_NAME + " ASC");
return cursor;
}
}
最后,这是我的WorkoutsFragment.java
package com.rcd.mypr.Workouts;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.rcd.mypr.R;
public class WorkoutsFragment extends Fragment {
WorkoutsDatabaseHelper db;
SimpleCursorAdapter adapter;
ListView listContent;
private int position;
Cursor cursor;
private Context mContext;
WorkoutsAdapter workoutsAdapter;
public WorkoutsFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
db = new WorkoutsDatabaseHelper(mContext);
mContext = getActivity();
View rootView = inflater.inflate(R.layout.activity_workouts, container, false);
listContent = (ListView) rootView.findViewById(android.R.id.list);
// Create the Adapter
workoutsAdapter = new WorkoutsAdapter(mContext, cursor);
// Set the adapter to ListView
listContent.setAdapter(workoutsAdapter);
return rootView;
}
private void initViews(){
//Typeface tf = Typeface.createFromAsset(mContext.getAssets(),"Roboto-Light.ttf");
}
private void addItemsToList(){
Cursor cursor = db.getWorkoutNames();
Log.d("history.java", "finished Cursor cursor = db.getAllLogs();");
String[] from = {"_id", "workouts_name"};
int[] to = {R.id.tv_id, R.id.tv_label};
adapter = new SimpleCursorAdapter(mContext, R.layout.single_item, cursor, from, to, 0);
listContent.setAdapter(adapter);
}
public void onResume(){
super.onResume();
workoutsAdapter = new WorkoutsAdapter(mContext, cursor);
listContent.setAdapter(workoutsAdapter);
}
}
如果您还需要查看其他内容,请告知我们。
答案 0 :(得分:2)
错误位于onCreateView()
的{{1}} - 方法中。
您使用Fragment
WorkoutsDatabaseHelper
初始化null
。
Context
切换这两行并再试一次......