SQlite数据库文件未加载

时间:2017-11-07 06:56:36

标签: java android database android-sqlite

我一直无法访问我的数据库文件,我不确定问题出在哪里,并且我已经查看了此错误的其他答案但没有成功。我认为这与主活动类没有在正确的时间创建IntDatabaseHelper的实例有关,但我一直在看所有内容,以至于我不确定什么是错的。我的所有代码和我的Logcat输出都在下面。

MainActivity

public class MainActivity extends AppCompatActivity {
IntDataBaseHelper intDataBaseHelper;

ListView lstJob;
ArrayAdapter<String> mAdapter;





   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

       intDataBaseHelper  = new IntDataBaseHelper(this);

    /*create instance of db helper and jobs
    Create the database (only if it doesn't exists)
    does so by copying from the assets */

    LoadJobList();
    if (CopyDBFromAssets.createDataBase(this,IntDataBaseHelper.DB_TABLE)) {
       // problem area
       // Get the data from the database
        lstJob = (ListView) findViewById(R.id.lstJob);
    ArrayList<String> jobs =  intDataBaseHelper.getJobList();
       for (String s : jobs) {
           Log.d("JobList ", "Found Job " + s);
          }
       } else {
           throw new RuntimeException("No Usable Database exists or was copied from the assets.");
       }

   }
  // loads job to screen
      public void LoadJobList() {
      ArrayList<String> JobList = intDataBaseHelper.getJobList();
      if (mAdapter == null) {
          mAdapter = new ArrayAdapter<>(this,R.layout.header,R.id.header);
          mAdapter = new ArrayAdapter<>(this,R.layout.row,R.id.BtnComplete,JobList);
          mAdapter = new ArrayAdapter<>(this, R.layout.row, R.id.Job_name,JobList);
          lstJob.setAdapter(mAdapter);
      } else
          {
          mAdapter.clear();
          mAdapter.addAll(JobList);
          mAdapter.notifyDataSetChanged();
      }
  }

}

IntDataBase

public class IntDataBaseHelper extends SQLiteOpenHelper{


private static  String DB_PATH ="//data/data/com.example.joelg.clapp/databases";
public static  String DB_NAME = "JobList.db";
public static  String DB_COLUMN = "jobNM";
public static  String DB_TABLE = "job";
private static  String DB_JOB_DETAILS = "jobDetails";
private static  String DB_ISDONE = "jobIsDone";
private  Context jobContext;
private SQLiteDatabase JobListDatabase;




    /**
     * constructor creater
     */
    public IntDataBaseHelper (Context context) {
        super (context, DB_NAME,null, 1);
        this.jobContext = context;
        DB_PATH = jobContext.getDatabasePath(DB_NAME).getPath();
    }


    public void OpenDataBase() {
        // open the database
        String JobListPath = DB_PATH;
        JobListDatabase = SQLiteDatabase.openDatabase(JobListPath,null,SQLiteDatabase.OPEN_READONLY);
    }


    // Getting Job Count
    public  ArrayList<String> getJobList() {
        ArrayList<String> JobList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor =  db.query(DB_TABLE,new String[]
                {DB_COLUMN},null,null,null,null,null);
            while(cursor.moveToNext()){
            int index = cursor.getColumnIndex(DB_COLUMN);
            JobList.add(cursor.getString(index));
        }

        cursor.close();
        db.close();
        return JobList;
    }


    // Gets the job state if it has been competed or not
public ArrayList<String> getIsDone() {
    ArrayList<String>  IsDone = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(DB_TABLE,new String[]{DB_ISDONE},null,null,null,null,null);
    while(cursor.moveToNext()){
        int index = cursor.getColumnIndex(DB_ISDONE);
        IsDone.add(cursor.getString(index));
    }

    cursor.close();
    db.close();
    return IsDone;
}




    @Override
    public synchronized void close(){

        if(JobListDatabase !=null){
            JobListDatabase.close();
            super.close();

        }
    }
    @Override
    public  void onCreate(SQLiteDatabase db) {

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

LogCat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joelg.clapp/com.example.joelg.clapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                     at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                     at android.os.Handler.dispatchMessage(Handler.java:105)
                                                     at android.os.Looper.loop(Looper.java:164)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                                     at com.example.joelg.clapp.MainActivity.LoadJobList(MainActivity.java:55)
                                                     at com.example.joelg.clapp.MainActivity.onCreate(MainActivity.java:34)
                                                     at android.app.Activity.performCreate(Activity.java:6975)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                     at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                     at android.os.Looper.loop(Looper.java:164) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     At com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

1 个答案:

答案 0 :(得分:1)

你的问题是

  

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'

那是lstJob.setAdapter(mAdapter);

所以你应该检查lstJob init。

你应该这样做。

LoadJobList方法后调用findViewById

lstJob = (ListView) findViewById(R.id.lstJob);
LoadJobList();

修改

您应该在onCreate课程中实施onUpdate方法和IntDataBaseHelper方法。

当你有

  

android.database.sqlite.SQLiteException:没有这样的表:job(代码1)

原因是当你添加一个表或修改表时,没有更新版本,在完成上述操作后,应该添加数据库版本,每次进行更改时,都应该添加一个。