public class DatabaseHundler {
Context context;
private static DatabaseHundler instance;
private static helper help;
public DatabaseHundler(Context context) {
this.context = context;
}
public static DatabaseHundler getInstance(Context context)
{
if(instance == null)
{
instance = new DatabaseHundler(context);
}
if(help == null)
{
help = instance.new helper();
}
return instance;
}
private synchronized static SQLiteDatabase open()
{
try{
return help.getWritableDatabase();
/*switch(mode) {
case WRITE_MODE:
return db.getWritableDatabase();
case READ_MODE:
return db.getReadableDatabase();
default:
return db.getWritableDatabase();
}*/
}catch (Exception e) {
return null;
}
}
private class helper extends SQLiteOpenHelper
{
public helper() {
super(context, Constants.Database_Name, null, Integer.parseInt(Constants.Database_Version));
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public boolean CreateTableMyData(String query)
{
SQLiteDatabase db = DatabaseHundler.open();
if(db != null)
{
db.execSQL(query);
}
return false;
}
public Cursor GetUserData(String tableName)
{
SQLiteDatabase db = DatabaseHundler.open();
if(db != null)
{
Cursor cursor = db.query(tableName, null, null, null, null, null, null);
try{
if(cursor != null)
{
if(cursor.getCount() >0)
{
return cursor;
}
}
}catch(Exception e){
}
}
return null;
}
public Cursor GetData(String tablename ,String colname , String val)
{
SQLiteDatabase db = DatabaseHundler.open();
if(db != null)
{
String query = "Select * from "+tablename+" where "+colname+" =? ";
Cursor cursor = db.rawQuery(query, new String[]{ val});
//Cursor cursor = db.query(tablename, null, null, null, null, null, null);
try{
if(cursor != null)
{
if(cursor.getCount() >0)
{
return cursor;
}
}
}catch(Exception e){
}
}
return null;
}
public boolean SaveUserData(ContentValues values , String tablename)
{
SQLiteDatabase dataBase = DatabaseHundler.open();
try{
if(dataBase != null)
{
if (values!= null)
{
dataBase.insertWithOnConflict(tablename, BaseColumns._ID, values, SQLiteDatabase.CONFLICT_REPLACE);
}
return true;
}
}catch(Exception e){}
return false;
}
public boolean DeleteData(String tablename)
{
SQLiteDatabase database = DatabaseHundler.open();
try{
if(database != null)
{
database.delete(tablename, null, null);
}
}catch(Exception e)
{
}
return false;
}
}
,例外是
01-30 11:24:20.991: E/AndroidRuntime(15714): FATAL EXCEPTION: main
01-30 11:24:20.991: E/AndroidRuntime(15714): Process: com.joyia.PriceFinder, PID: 15714
01-30 11:24:20.991: E/AndroidRuntime(15714): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joyia.PriceFinder/com.joyia.PriceFinder.Splash}: java.lang.NullPointerException
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.os.Handler.dispatchMessage(Handler.java:102)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.os.Looper.loop(Looper.java:136)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-30 11:24:20.991: E/AndroidRuntime(15714): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:24:20.991: E/AndroidRuntime(15714): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 11:24:20.991: E/AndroidRuntime(15714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-30 11:24:20.991: E/AndroidRuntime(15714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-30 11:24:20.991: E/AndroidRuntime(15714): at dalvik.system.NativeStart.main(Native Method)
01-30 11:24:20.991: E/AndroidRuntime(15714): Caused by: java.lang.NullPointerException
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
01-30 11:24:20.991: E/AndroidRuntime(15714): at com.joyia.PriceFinder.DatabaseHundler$helper.<init>(DatabaseHundler.java:52)
01-30 11:24:20.991: E/AndroidRuntime(15714): at com.joyia.PriceFinder.DatabaseHundler.getInstance(DatabaseHundler.java:30)
01-30 11:24:20.991: E/AndroidRuntime(15714): at com.joyia.PriceFinder.Controller.<init>(Controller.java:9)
01-30 11:24:20.991: E/AndroidRuntime(15714): at com.joyia.PriceFinder.Splash.onCreate(Splash.java:34)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.app.Activity.performCreate(Activity.java:5231)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-30 11:24:20.991: E/AndroidRuntime(15714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-30 11:24:20.991: E/AndroidRuntime(15714): ... 11 more
////////泼水活动
public class Splash extends Activity
{
static String DeviceId;
static String Key;
static String Email;
static String Version;
static String status;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Controller();
Register register = Controller.hundler.retriveRegisterData();
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
DeviceId = telephonyManager.getDeviceId();
Key= Utility.KeyGenration(DeviceId);
try {
Version = "v"+getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(register == null)
{
final AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setTitle(getString(R.string.app_name));
dialog.setMessage("Enter Email here");
LayoutInflater inflate = this.getLayoutInflater();
View view = inflate.inflate(R.layout.dialog_layout, null);
dialog.setView(view);
dialog.show();
final EditText email = (EditText) view.findViewById(R.id.editText1);
Button ok = (Button) view.findViewById(R.id.button1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Email = email.getText().toString();
RefrashTask task = new RefrashTask();
task.execute();
dialog.dismiss();
}
});
}
}
////////////////我们制作数据库hundler的控制器类
public class Controller extends Application
{
public static DatabaseHundler hundler ;
public Controller() {
hundler = DatabaseHundler.getInstance(this);
}
}
答案 0 :(得分:2)
传递给数据库处理程序的上下文不是有效的上下文。
Controller
是Application
,但在您的活动onCreate()
中,您使用new
初始化它。这是禁忌。您应该通过在清单中声明它来初始化Application
类。您可以在Application
中使用Context
作为onCreate()
,但不能在{{1}}中使用{{1}}。在构造函数中。