2015-10-16 42 views
0

我是剛接觸android並在從SQLiteeditText檢索數據時遇到問題。當該行被按下時,應用程序崩潰。有誰知道如何解決這一問題?是不是因爲cursor未初始化或因爲sql問題?希望有人能幫我弄清楚這個問題..非常感謝。無法從CursorWindow讀取第1行,列-1。在訪問數據之前確保光標已正確初始化

c = database.rawQuery("SELECT i.Name, i.Date, i.Status, i.Weather, w.Subcontractors, w.NumberOfPerson, w.NumberOfHours FROM Information i LEFT JOIN WorkForce w ON w.TInfo_id = i.ID" + 
         " LEFT JOIN WorkDetails wd ON wd.Twf_id = w.ID WHERE i.Name = ? AND i.Date= ? ", 

UpdatePage.java

包com.example.project.project;

import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.Toast; 

import com.example.project.project.API.InfoAPI; 
import com.example.project.project.TimeSheet.Details; 
import com.example.project.project.TimeSheet.Force; 
import com.example.project.project.TimeSheet.Info; 
import com.example.project.project.database.MyDatabaseHelper; 


public class UpdatePage extends AppCompatActivity { 
    InfoAPI sqlcon; 
    private SQLiteDatabase database; 
    private MyDatabaseHelper dbHelper; 
    private Cursor c; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     dbHelper = new MyDatabaseHelper(this); 
     setContentView(R.layout.updatepage); 
     final String name1 = getIntent().getExtras().getString("name"); 
     final String date = getIntent().getExtras().getString("date"); 
     RetrievePage(name1, date); 
    } 


    public void RetrievePage(String name, String date) { 
     final String name2 = name; 
     final String date2 = date; 
     database = dbHelper.getWritableDatabase(); 
     c = database.rawQuery("SELECT i.Name, i.Date, i.Status, i.Weather, w.Subcontractors, w.NumberOfPerson, w.NumberOfHours FROM Information i LEFT JOIN WorkForce w ON w.TInfo_id = i.ID LEFT JOIN WorkDetails wd ON wd.Twf_id = w.ID WHERE i.Name = ? AND i.Date= ? ", 
       new String[]{String.valueOf(name2),String.valueOf(date2)}, null); 
     final EditText name3 = (EditText) findViewById(R.id.editText9); 
     final EditText date3 = (EditText) findViewById(R.id.editText12); 
     name3.setText(name2); 
     date3.setText(date2); 
     final Spinner weather3 = (Spinner) findViewById(R.id.spinner5); 
     final Spinner status3 = (Spinner) findViewById(R.id.spinner7); 
     final EditText subC3 = (EditText) findViewById(R.id.editText17); 
     final EditText noP = (EditText) findViewById(R.id.editText18); 
     final EditText noH = (EditText) findViewById(R.id.editText19); 
     final Spinner poject3 = (Spinner) findViewById(R.id.spinner8); 
     if (c != null) { 
      c.moveToFirst(); 
      while (c.moveToNext()) { 
       Info I = new Info(); 
       Force WF = new Force(); 
       Details WD = new Details(); 

       String Weather = c.getString(c.getColumnIndex(MyDatabaseHelper.Weather)); 
       String Status = c.getString(c.getColumnIndex(MyDatabaseHelper.Status)); 
       String SubC = c.getString(c.getColumnIndex(MyDatabaseHelper.Subcontractors)); 
       String NoP = c.getString(c.getColumnIndex(MyDatabaseHelper.NumberOfPerson)); 
       String NoH = c.getString(c.getColumnIndex(MyDatabaseHelper.NumberOfHours)); 
       String Project = c.getString(c.getColumnIndex(MyDatabaseHelper.Project)); 


       //I.setWeather(Weather); 
       // I.setStatus(Status); 
       WF.setSubcontractors(SubC); 
       WF.setNoOfPerson(NoP); 
       WF.setNoOfHours(NoH); 
       //WD.setProject(Project); 


       subC3.setText(SubC); 
       noP.setText(NoP); 
       noH.setText(NoH); 

      } 

     } 

    } 
    } 

錯誤的logcat

ComponentInfo{com.example.project.project/com.example.project.project.UpdatePage}: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:148) 
       at android.app.ActivityThread.main(ActivityThread.java:5417) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
       at android.database.CursorWindow.nativeGetString(Native Method) 
       at android.database.CursorWindow.getString(CursorWindow.java:438) 
+0

嘗試'getColumnIndexorThrow()',還要確保關閉你的'Cursor'和'SQLiteDatabase'對象 –

+0

除此之外,這是正確的方式來顯示檢索到的數據textView? – Hoo

+0

要問一個問題,請使用「提問」按鈕。 –

回答

0

moveToFirst()移動到第一行。 moveToNext()然後移動到第二行。

但是你的問題是缺少一列。 getColumnIndex()將在您不知道列是否存在時使用。 要獲得您假定的列的索引,請使用getColumnIndexOrThrow(),以便您立即在錯誤消息中看到您在查詢中忘記了哪一列。

相關問題