Android Cant执行更新查询

时间:2014-08-18 17:09:56

标签: android sqlite

我在这方面有点新,所以,我正在尝试执行更新查询,以根据在无线电组中进行的选择来更改字段的值。然而,它总是给我一个NullPointerException,我还没有能够确切地找出原因。这是代码:

GenderFragment:

package com.example.yipeedo;


import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class GenderFragment extends Fragment 
{
int mCurrentPage;
Button next;
RadioGroup genderGroup;
int x;
String choice;
RadioButton gender;
View v;
public SQLiteDatabase db;
DataBaseHelper dbh;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Getting the arguments to the Bundle object */
    Bundle data = getArguments();

    /** Getting integer data of the key current_page from the bundle */
    mCurrentPage = data.getInt("current_page", 0);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.gender_layout, container,false);
    next=(Button)v.findViewById(R.id.next);
    genderGroup=(RadioGroup)v.findViewById(R.id.GenderGroup);

    next.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            updateDatabase();
            ((PreferencesActivity)getActivity()).setCurrentItem(1, true);

        }

    });
    return v;
}

public void updateDatabase()
{
    int selectedId=genderGroup.getCheckedRadioButtonId();
    gender=(RadioButton)v.findViewById(selectedId);
    String selection=gender.getText().toString();
    Log.e("Add Gender To Database","Gender:"+selection);
    try
    {

    //dbh.createDataBase();
    //dbh.openDataBase();
    dbh.updateGender(selection);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}


}

DataBaseHelper:

package com.example.yipeedo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

 public class DataBaseHelper extends SQLiteOpenHelper{


private static String DB_PATH = "/data/data/com.example.yipeedo/databases/";

private static String DB_NAME = "YipeedoDB1.txt";

private SQLiteDatabase myDataBase; 

private final Context myContext;
InputStream is;


public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
}   

public void write(InputStream is)
{
    try {
        OutputStream out = new FileOutputStream(new File(DB_PATH));
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = is.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        is.close();
        out.flush();
        out.close();
        System.err.println(out + "\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}


@Override
public void onCreate(SQLiteDatabase db) 
{
    try 
    {
        is=myContext.getAssets().open("YipeedoDB1.txt");
        write(is);
        myDataBase.openOrCreateDatabase(DB_PATH+DB_NAME, null);
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{

}

public void updateGender(String x)
{

    myDataBase.execSQL("Update UserData set gender = " + x + " where name like 'ram';");

}



}

logcat的:

08-18 22:34:46.088: W/System.err(17086): java.lang.NullPointerException
08-18 22:34:46.108: W/System.err(17086):    at     com.example.yipeedo.GenderFragment.updateDatabase(GenderFragment.java:71)
08-18 22:34:46.108: W/System.err(17086):    at com.example.yipeedo.GenderFragment$1.onClick(GenderFragment.java:51)
08-18 22:34:46.108: W/System.err(17086):    at android.view.View.performClick(View.java:4432)
08-18 22:34:46.108: W/System.err(17086):    at android.view.View$PerformClick.run(View.java:18338)
08-18 22:34:46.108: W/System.err(17086):    at android.os.Handler.handleCallback(Handler.java:725)
08-18 22:34:46.108: W/System.err(17086):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 22:34:46.108: W/System.err(17086):    at android.os.Looper.loop(Looper.java:137)
08-18 22:34:46.108: W/System.err(17086):    at android.app.ActivityThread.main(ActivityThread.java:5283)
08-18 22:34:46.108: W/System.err(17086):    at java.lang.reflect.Method.invokeNative(Native Method)
08-18 22:34:46.108: W/System.err(17086):    at java.lang.reflect.Method.invoke(Method.java:511)
08-18 22:34:46.108: W/System.err(17086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-18 22:34:46.108: W/System.err(17086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-18 22:34:46.108: W/System.err(17086):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

问题是变量dbh,为null。

on onCreate方法添加:

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

    /** Getting the arguments to the Bundle object */
    Bundle data = getArguments();

    /** Getting integer data of the key current_page from the bundle */
    mCurrentPage = data.getInt("current_page", 0);

    dbh = new DataBaseHelper(getActivity());
}

并修改:

public void updateGender(String x)
{
    if(myDataBase == null)
        myDataBase = getWritableDatabase();

    myDataBase.execSQL("Update UserData set gender = " + x + " where name like 'ram';");

}

在调用DBOpenHelper的onCreate方法之前,您需要将数据库从资产复制到内部存储,请重新考虑您的类实现。以下是实施What are the best practices for SQLite on Android?的一个很好的例子。

答案 1 :(得分:0)

您需要实例化dbh:

dbh = new DataBaseHelper(yourcontext) // Somewhere in the creation of the fragment.

如果您需要适当的上下文,可以使用父活动,该活动在片段的onCreateView中传递给您。