2014-01-30 78 views
3
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; 
    } 

} 

和異常是SQLiteDatabase通過NullPointerException異常

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

顯示您的Splash類。 –

+0

@Ahmad向我們提供代碼,試圖製作數據庫實例或嘗試打開它。 –

+0

你爲什麼從你的Activity中調用應用程序類?你有沒有在清單中將你的'Controller'類定義爲應用程序?應用程序類將始終首先啓動,然後將啓動活動。 – GrIsHu

回答

2

您所遇到的上下文d到數據庫處理程序不是有效的上下文。

Controller is-a Application但在您的活動onCreate()中,您用new初始化它。這是一個禁忌。您應該在清單中聲明它,以初始化您的Application類。您可以在onCreate()中使用Application作爲Context,但不能早於在構造函數中。