我正在尝试将数据库中的数据插入到DecisionDatabase
类中。但是当涉及到线时,ourDatabase.insert(DB_TABLE, null, cv);
它会抛出一个NullPointerException
. 它不打开DecisionDatabase
类日志说:Couldn't open database aySonuDecisionDB.sql
。我试图找出为什么它不打开数据库,但找不到。这是应用程序的主要类。
public class Main extends FragmentActivity {
EditText bakici, arabaKredisi, ogrenimKredisi, tatilKredisi, elektrik;
EditText su, internet, aidat, kasko, digerTaksitler;
EditText diger, maasSelo, maasHilal, digerGelirler;
TextView toplamGelir, toplamHarcama, eldeKalan;
Spinner yil, ay;
LinearLayout row17, row18, row19;
TemporaryDatabase tdb;
AySonuDatabase aDb;
DecisionDatabase dDb;
MainFragment mainFragment = new MainFragment();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();;
private final String TAG = this.getClass().getSimpleName();
int toplamgelir, toplamharcama = 0, eldekalan = 0;
int bakicii, arabakredisi, ogrenimkredisi, tatilkredisi, elektrikk;
int suu, internett, aidatt, kaskoo, digergelirler, maasselo, maashilal;
int digertaksitler, digerr, yil2;
String ay2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Widget tanımlamaları
toplamGelir = (TextView) findViewById(R.id.editTextToplamGelir);
digerGelirler = (EditText) findViewById(R.id.editTextDigerGelirler);
toplamHarcama = (TextView) findViewById(R.id.editTextToplamHarcama);
diger = (EditText) findViewById(R.id.editTextDiger);
digerTaksitler = (EditText) findViewById(R.id.editTextDigerTaksitler);
kasko = (EditText) findViewById(R.id.editTextKaskoSigorta);
aidat = (EditText) findViewById(R.id.editTextAidat);
internet = (EditText) findViewById(R.id.editTextFaturaInternet);
su = (EditText) findViewById(R.id.editTextFaturaSu);
elektrik = (EditText) findViewById(R.id.editTextFaturaElektrik);
tatilKredisi = (EditText) findViewById(R.id.editTextTatilKredisi);
ogrenimKredisi = (EditText) findViewById(R.id.editTextOgrenimKredisi);
arabaKredisi = (EditText) findViewById(R.id.editTextKrediAraba);
bakici = (EditText) findViewById(R.id.editTextBakici);
eldeKalan = (TextView) findViewById(R.id.editTextEldeKalan);
maasSelo = (EditText) findViewById(R.id.editTextMaasSelo);
maasHilal = (EditText) findViewById(R.id.editTextMaasHilal);
yil = (Spinner) findViewById(R.id.spinnerYillar);
ay = (Spinner) findViewById(R.id.spinnerAylar);
row17 = (LinearLayout) findViewById(R.id.tableRow17);
row18 = (LinearLayout) findViewById(R.id.tableRow18);
row19 = (LinearLayout) findViewById(R.id.tableRow19);
// Button etki tepkileri
Button hesapla, getir, kaydet;
hesapla = (Button) findViewById(R.id.buttonHesapla);
getir = (Button) findViewById(R.id.buttonGetir);
kaydet = (Button) findViewById(R.id.buttonKaydet);
kaydet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
kaydeden();
}
});
hesapla.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hesaplayan();
}
});
getir.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int yil2 = Integer.valueOf(yil.getSelectedItem().toString());
String ay2 = ay.getSelectedItem().toString();
tdb.Entry(yil2, ay2);
row17.setVisibility(1);
row18.setVisibility(1);
row19.setVisibility(1);
if (mainFragment != null)
transaction.remove(mainFragment);
fragment();
}
});
Log.i(TAG, "mainonCreate");
}
// Bütçe hesaplamalarını yapan yordam
protected void hesaplayan() {
bakicii = Integer.valueOf(bakici.getText().toString());
arabakredisi = Integer.valueOf(arabaKredisi.getText().toString());
ogrenimkredisi = Integer.valueOf(ogrenimKredisi.getText().toString());
tatilkredisi = Integer.valueOf(tatilKredisi.getText().toString());
elektrikk = Integer.valueOf(elektrik.getText().toString());
suu = Integer.valueOf(su.getText().toString());
internett = Integer.valueOf(internet.getText().toString());
aidatt = Integer.valueOf(aidat.getText().toString());
kaskoo = Integer.valueOf(kasko.getText().toString());
digergelirler = Integer.valueOf(digerGelirler.getText().toString());
maasselo = Integer.valueOf(maasSelo.getText().toString());
maashilal = Integer.valueOf(maasHilal.getText().toString());
digertaksitler = Integer.valueOf(digerTaksitler.getText().toString());
digerr = Integer.valueOf(diger.getText().toString());
toplamharcama = bakicii + arabakredisi + ogrenimkredisi + tatilkredisi
+ elektrikk + suu + internett + aidatt + kaskoo
+ digertaksitler + digerr;
toplamHarcama.setText(String.valueOf(toplamharcama));
toplamgelir = maashilal + maasselo + digergelirler;
toplamGelir.setText(String.valueOf(toplamgelir));
eldekalan = toplamgelir - toplamharcama;
eldeKalan.setText(String.valueOf(eldekalan));
row17.setVisibility(0);
row18.setVisibility(0);
row19.setVisibility(0);
Log.i(TAG, "mainHesaplayan");
}
// Bütçe hesaplamalarını veritabanına kaydeden yordam.
protected void kaydeden() {
boolean didItWork = false;
boolean isDataInDatabase = false;
int i;
try {
yil2 = Integer.valueOf(yil.getSelectedItem().toString());
ay2 = ay.getSelectedItem().toString();
isDataInDatabase = aDb.Entry(yil2, ay2, bakicii, arabakredisi,
ogrenimkredisi, tatilkredisi, elektrikk, suu, internett,
aidatt, kaskoo, digertaksitler, digerr, maasselo,
maashilal, digergelirler, toplamharcama, toplamgelir,
eldekalan);
if (isDataInDatabase) {
FragmentManager fm = getSupportFragmentManager();
MyDialogFragment testDialog = new MyDialogFragment();
testDialog.setRetainInstance(true);
testDialog.show(fm, "MyDialogFragment");
i = dDb.getDecisionData();
switch (i) {
case 0:
aDb.EntryWithoutDialog(yil2, ay2, bakicii, arabakredisi,
ogrenimkredisi, tatilkredisi, elektrikk, suu,
internett, aidatt, kaskoo, digertaksitler, digerr,
maasselo, maashilal, digergelirler, toplamharcama,
toplamgelir, eldekalan);
didItWork = true;
break;
default:
break;
}
} else
didItWork = true;
} catch (Exception e) {
didItWork = false;
Log.i("Log", e.getMessage() + "Error!");
} finally {
if (didItWork)
Toast.makeText(Main.this, "DBEntrySucceed", Toast.LENGTH_LONG)
.show();
else
Toast.makeText(Main.this, "NoDBEntry", Toast.LENGTH_LONG)
.show();
Log.i(TAG, "mainKaydeden");
}
}
// Seçilen ay ve yılın bütçesini ekrana fragment yardımıyla yazdıran yordam.
public void fragment() {
try {
transaction.add(R.id.mainLinLay, mainFragment);
transaction.addToBackStack(null);
transaction.commit();
// Log
Log.i(TAG, "mainfragment");
} catch (Exception e) {
// TODO: handle exception
Log.i(TAG, "mainfragmentException");
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
aDb = new AySonuDatabase(Main.this);
aDb.open();
tdb = new TemporaryDatabase(Main.this);
tdb.open();
dDb = new DecisionDatabase(Main.this);
dDb.open();
Log.i(TAG, "mainonResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
tdb.close();
aDb.close();
dDb.close();
Log.i(TAG, "mainonPause");
}
和片段类;
public class MyDialogFragment extends DialogFragment{
Context mContext=getActivity();
boolean decision;
DecisionDatabase dDb=new DecisionDatabase(mContext);
int i;
private final String TAG = this.getClass().getSimpleName();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("Aynı tarihli kayit var!!!");
alertDialogBuilder.setMessage("Kaydetmek istediğinizden emin misiniz?");
dDb.open();
//null should be your on click listener
alertDialogBuilder.setPositiveButton("Evet", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
i=0;
dDb.delete();
dDb.EntryDecision(i);
dDb.close();
dialog.dismiss();
}
});
alertDialogBuilder.setNegativeButton("Hayır", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
i=1;
dDb.delete();
dDb.EntryDecision(i);
dDb.close();
dialog.dismiss();
}
});
Log.i(TAG, "MyDialogFragmentOnCreateDialog");
return alertDialogBuilder.create();
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
dDb.close();
Log.i(TAG, "MyDialogFragmentOnStop");
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG, "MyDialogFragmentOnStart");
}
}
和决策数据库类;
public class DecisionDatabase {
public static final String TAG = DbHelperTemporary2.class.getSimpleName();
public static final String DB_NAME = "aySonuDecisionDB.sql";
private static int DB_PATH = R.string._data_data_com_bilge_aysonu_assets_;
public static final int DB_VERSION = 1;
public static final String DB_TABLE = "decision";
public static final String C_ID = "_id";// Special for id
public static final String C_DECISION = "dialogDecision";
public static final String CREATE_TABLE = "CREATE TABLE decision (_id integer primary key autoincrement, "
+ "dialogDecision integer);";
private DbHelperTemporary2 dbHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public static class DbHelperTemporary2 extends SQLiteOpenHelper {
public DbHelperTemporary2(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "onCreate sql: " + CREATE_TABLE);
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + DB_TABLE);
Log.w(TAG, "onUpdate drop table " + DB_TABLE);
onCreate(db);
}
}
public DecisionDatabase(Context c) {
super();
ourContext = c;
Log.i(TAG, "DecisionDatabaseConstructor");
}
public void openDatabase() throws SQLException {
ourDatabase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
SQLiteDatabase.OPEN_READWRITE);
}
public void open() throws NumberFormatException {
try {
dbHelper = new DbHelperTemporary2(ourContext);
ourDatabase = dbHelper.getWritableDatabase();
Log.i(TAG, "DecisionDatabaseOpen");
return;
} catch (Exception e) {
Log.d(TAG, "Couldn't open database " + DB_NAME);
e.printStackTrace();
} finally {
}
}
public void close() {
dbHelper.close();
ourDatabase.close();
}
public void delete (){
if(ourDatabase!=null)
ourDatabase.delete(DB_TABLE, null, null);
}
public void EntryDecision(int decision) {
int decisionentry=decision;
ContentValues cv = new ContentValues();
cv.put(C_DECISION, decisionentry);
ourDatabase.insert(DB_TABLE, null, cv);
cv.clear();
Log.i(TAG, "DecisionDatabaseEntryDecision");
}
public int getDecisionData() {
Cursor c = ourDatabase.query(DB_TABLE, null, null, null, null, null,
null);
String[] result = new String[1];
int i_decision = c.getColumnIndex(C_DECISION);
if (c.moveToFirst()) {
result[0] = c.getString(i_decision);
} else
result[0] = "2";
c.close();
Log.i(TAG, "DecisionDatabaseGetDecisionData");
return Integer.parseInt(result[0]);
}
}
这是日志和异常;
06-08 11:16:37.869: I/Main(326): mainonCreate
06-08 11:16:37.909: I/DatabaseHelper(326): AysonuDatabaseConstructor
06-08 11:16:38.351: D/DatabaseHelper(326): onCreate sql: CREATE TABLE harcamalar (c_id integer primary key autoincrement,spinnerYillar integer,spinnerAylar TEXT,editTextBakici integer,editTextKrediAraba integer,editTextKrediOgrenim integer,editTextKrediTatil integer,editTextFaturaElektrik integer,editTextFaturaSu integer,editTextFaturaInternet integer,editTextAidat integer,editTextKaskoSigorta integer,editTextDigerTaksitler integer,editTextDiger integer,editTextMaasSelo integer,editTextMaasHilal integer,editTextDigerGelirler integer,editTextToplamHarcama integer,editTextToplamGelir integer, editTextEldeKalan integer, UNIQUE (spinnerYillar,spinnerAylar) ON CONFLICT REPLACE);
06-08 11:16:38.369: I/DatabaseHelper(326): AysonuDatabaseOncreate
06-08 11:16:38.399: I/DatabaseHelper(326): AysonuDatabaseOpen
06-08 11:16:38.409: I/DbHelperTemporary(326): TemporaryDatabaseConstructor
06-08 11:16:38.579: D/DbHelperTemporary(326): onCreate sql: CREATE TABLE ayyil (_id integer primary key autoincrement,spinnerYillar integer,spinnerAylar TEXT, dialogDecision integer);
06-08 11:16:38.639: I/DbHelperTemporary(326): TemporaryDatabaseOpen
06-08 11:16:38.669: I/DbHelperTemporary2(326): DecisionDatabaseConstructor
06-08 11:16:38.929: D/DbHelperTemporary2(326): onCreate sql: CREATE TABLE decision (_id integer primary key autoincrement, dialogDecision integer);
06-08 11:16:38.990: I/DbHelperTemporary2(326): DecisionDatabaseOpen
06-08 11:16:38.999: I/Main(326): mainonResume
06-08 11:17:50.229: W/KeyCharacterMap(326): No keyboard for id 0
06-08 11:17:50.240: W/KeyCharacterMap(326): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-08 11:18:42.569: I/Main(326): mainHesaplayan
06-08 11:19:32.549: I/DatabaseHelper(326): AysonuDatabaseEntryElse
06-08 11:19:32.639: I/Main(326): mainKaydeden
06-08 11:19:40.059: I/DatabaseHelper(326): AysonuDatabaseEntryIf
06-08 11:19:40.069: I/DbHelperTemporary2(326): DecisionDatabaseConstructor
06-08 11:19:40.119: I/DbHelperTemporary2(326): DecisionDatabaseGetDecisionData
06-08 11:19:40.169: I/Main(326): mainKaydeden
06-08 11:19:40.190: D/DbHelperTemporary2(326): Couldn't open database aySonuDecisionDB.sql
06-08 11:19:40.190: W/System.err(326): java.lang.NullPointerException
06-08 11:19:40.209: W/System.err(326): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
06-08 11:19:40.209: W/System.err(326): at com.bilge.aysonu.DecisionDatabase.open(DecisionDatabase.java:69)
06-08 11:19:40.219: W/System.err(326): at com.bilge.aysonu.MyDialogFragment.onCreateDialog(MyDialogFragment.java:23)
06-08 11:19:40.239: W/System.err(326): at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
06-08 11:19:40.239: W/System.err(326): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-08 11:19:40.249: W/System.err(326): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-08 11:19:40.269: W/System.err(326): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-08 11:19:40.289: W/System.err(326): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-08 11:19:40.289: W/System.err(326): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
06-08 11:19:40.309: W/System.err(326): at android.os.Handler.handleCallback(Handler.java:587)
06-08 11:19:40.309: W/System.err(326): at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 11:19:40.330: W/System.err(326): at android.os.Looper.loop(Looper.java:123)
06-08 11:19:40.330: W/System.err(326): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-08 11:19:40.339: W/System.err(326): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 11:19:40.339: W/System.err(326): at java.lang.reflect.Method.invoke(Method.java:521)
06-08 11:19:40.360: W/System.err(326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-08 11:19:40.360: W/System.err(326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-08 11:19:40.369: W/System.err(326): at dalvik.system.NativeStart.main(Native Method)
06-08 11:19:40.383: I/MyDialogFragment(326): MyDialogFragmentOnCreateDialog
06-08 11:19:40.839: I/MyDialogFragment(326): MyDialogFragmentOnStart
06-08 11:38:26.058: E/AndroidRuntime(326): FATAL EXCEPTION: main
06-08 11:38:26.058: E/AndroidRuntime(326): java.lang.NullPointerException
06-08 11:38:26.058: E/AndroidRuntime(326): at com.bilge.aysonu.DecisionDatabase.EntryDecision(DecisionDatabase.java:94)
06-08 11:38:26.058: E/AndroidRuntime(326): at com.bilge.aysonu.MyDialogFragment$2.onClick(MyDialogFragment.java:44)
06-08 11:38:26.058: E/AndroidRuntime(326): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
06-08 11:38:26.058: E/AndroidRuntime(326): at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 11:38:26.058: E/AndroidRuntime(326): at android.os.Looper.loop(Looper.java:123)
06-08 11:38:26.058: E/AndroidRuntime(326): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-08 11:38:26.058: E/AndroidRuntime(326): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 11:38:26.058: E/AndroidRuntime(326): at java.lang.reflect.Method.invoke(Method.java:521)
06-08 11:38:26.058: E/AndroidRuntime(326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-08 11:38:26.058: E/AndroidRuntime(326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-08 11:38:26.058: E/AndroidRuntime(326): at dalvik.system.NativeStart.main(Native Method)
感谢您的帮助...