2017-11-07 16 views
0

データベースファイルへのアクセスに問題がありましたが、どこに問題があるのか​​正確にはわかりません。私はそれが、主なアクティビティクラスで、適切なタイミングでIntDatabaseHelperのインスタンスを作成しないことと関係していると思っていますが、何が間違っているのか正確には分かりません。すべての私のコードと私のLogcatの出力は以下の通りです。SQliteデータベースファイルがロードされていません

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) 
+2

[NullPointerExceptionとは何ですか?どうすれば修正できますか?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix - ) –

答えて

1

あなたの問題は

01です

に起因:java.lang.NullPointerExceptionが:NULLオブジェクト参照に 'ボイドandroid.widget.ListView.setAdapter(android.widget.ListAdapter)' 仮想メソッドを起動しようと

lstJob.setAdapter(mAdapter);でした。

lstJob initをチェックする必要があります。

これを行う必要があります。

findViewByIdメソッドの後にLoadJobListを呼び出します。

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

編集

あなたはonCreate方法と、あなたのIntDataBaseHelperクラスのonUpdateメソッドを実装する必要があります。そのようなテーブル::ジョブ(コード1)

理由は、あなたがテーブルを追加したり、テーブルを変更する場合ということです、あなたは

android.database.sqlite.SQLiteExceptionを持っている場合

上記の操作を行った後、更新バージョンはなく、データベースバージョンを追加する必要があります。変更するたびに追加する必要があります。

+0

あなたの投票権を使用して**農業評判を止めてください** –

+0

これはありがとうございます "android.database.sqlite.SQLiteException:このようなテーブルはありません:ジョブ(コード1):、コンパイル中:SELECT jobNM FROM job " –

+0

農場の評判に意味はありません –

関連する問題