如何在片段中使用sqlite

时间:2015-04-17 15:46:38

标签: android sqlite android-fragments

我无法在任何地方找到可以帮助我在片段中使用sqlite db的教程。我制作数据库时感到非常沮丧,当我点击保存按钮时,它会崩溃。我需要做些什么才能让它发挥作用?

公共类Tab3扩展了Fragment {

Button save, reset, load;
EditText logl, logd, logid;
DataHandler handler;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab_3, container, false);
    return v;


}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    save = (Button) getActivity().findViewById(R.id.buttonSave);
    load = (Button) getActivity().findViewById(R.id.buttonShow);
    reset = (Button) getActivity().findViewById(R.id.reset);
    logl = (EditText) getActivity().findViewById(R.id.logLength);
    logd = (EditText) getActivity().findViewById(R.id.logDiameter);
    logid = (EditText) getActivity().findViewById(R.id.logId);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String getLoglength = logl.getText().toString();
            String getLogdiameter = logd.getText().toString();
            String getLogid = logid.getText().toString();
            handler = new DataHandler(getActivity());
            handler.open();
            long id = handler.insertData(getLoglength, getLogdiameter,getLogid);
            Toast.makeText(getActivity(), "Data Inserted", Toast.LENGTH_LONG).show();
            handler.close();
        }
    });

    load.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String getLoglength, getLogdiameter, getLogid;
            getLoglength = "";
            getLogdiameter = "";
            getLogid = "";
            handler = new DataHandler(getActivity());
            handler.open();
            Cursor C = handler.returnData();
            if (C.moveToFirst()){

            }
            do{
                getLoglength = C.getString(0);
                getLogdiameter = C.getString(1);
                getLogid = C.getString(2);

            }
            while (C.moveToNext());

            handler.close();
            Toast.makeText(getActivity(), "Log Length:" +getLoglength+ "Log Diameter:  " +getLogdiameter+ "Log ID:  " +getLogid, Toast.LENGTH_LONG).show();
        }
    });
}

}

这是我的Datahandler

    public class DataHandler {


    public static final String LOGLENGTH = "logl";
    public static final String LOGDIAMETER = "logd";
    public static final String LOGID = "logid";
    public static final String TABLE_NAME = "mylogtable";
    public static final String DATABASE_NAME = "mydatabase";
    public static final int DATABASE_VERSION = 1;
    private static final String TABLE_CREATE = "CREATE TABLE "+TABLE_NAME+" (_id INTEGER PRIMARY KEY AUTOINCREMENT, "+LOGLENGTH+" VARCHAR(255), "+LOGDIAMETER+" VARCHAR(255), "+LOGID+" VARCHAR(255));";

    DatabaseHelper dbhelper;
    Context ctx;
    SQLiteDatabase db;

    public DataHandler(Context ctx) {
        this.ctx = ctx;
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context ctx) {
            super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(TABLE_CREATE);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }


        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS mylogtable");
            onCreate(db);
        }

    }

    public DataHandler open() {
        db = dbhelper.getWritableDatabase();
        return this;
    }

    public void close(){
        dbhelper.close();
    }

    public long insertData (String logl, String logd, String logid){
        ContentValues content = new ContentValues();
        content.put(LOGLENGTH, logl);
        content.put(LOGDIAMETER, logd);
        content.put(LOGID, logid);
        return db.insertOrThrow(TABLE_NAME, null, content);
    }

    public Cursor returnData(){
        return db.query(TABLE_NAME, new String[] {LOGID, LOGLENGTH, LOGDIAMETER}, null, null, null, null, null);
    }
}

这是我的logcat:

4-17 14:57:33.003    8263-8263/com.example.dpivl.teak101 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4174fd58)
04-17 14:57:33.023    8263-8263/com.example.dpivl.teak101 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.dpivl.teak101, PID: 8263
    java.lang.NullPointerException
            at com.example.dpivl.teak101.DataHandler.open(DataHandler.java:59)
            at com.example.dpivl.teak101.Tab3$1.onClick(Tab3.java:51)
            at android.view.View.performClick(View.java:4496)
            at android.view.View$PerformClick.run(View.java:18603)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5426)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
04-17 14:57:33.023     773-1280/? D/DisplayManagerGlobal﹕ getDisplayInfo: displayId=0, info=DisplayInfo{"Built-in Screen", app 540 x 960, real 540 x 960, largest app 960 x 922, smallest app 540 x 502, 60.0 fps, rotation0, density 240 (221.225 x 221.672) dpi, layerStack 0, type BUILT_IN, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}
04-17 14:57:33.023     773-1280/? W/ActivityManager﹕ Force finishing activity com.example.dpivl.teak101/.MainActivity
04-17 14:57:33.

0 个答案:

没有答案