0

sqlite から多くのテキストビューにデータを表示するのに問題があります。たとえば、名前については、textviewName に置きたいと思います。以下は私のコードです。

protected void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile);

    TextView TV_Name = (TextView)findViewById(R.id.TVName);
    TextView TV_DOB = (TextView)findViewById(R.id.TVDOB);
    TextView TV_Address = (TextView)findViewById(R.id.TVAddress);
    TextView TV_BloodType = (TextView)findViewById(R.id.TVBloodType);
    TextView TV_Height =    (TextView)findViewById(R.id.TVHeight);
    TextView TV_DrName =    (TextView)findViewById(R.id.TVDrName);
    TextView TV_DrContact = (TextView)findViewById(R.id.TVDrContact);
    TextView TV_CaretakerName = (TextView)findViewById(R.id.TVCaretakerName);
    TextView TV_CaretakerContact = (TextView)findViewById(R.id.TVCaretakerContact);
    TextView TV_HospitalName = (TextView)findViewById(R.id.TVHospitalName);
    TextView TV_HospitalContact = (TextView)findViewById(R.id.TVHospitalContact);

    DBHelper helper = new DBHelper(this);
    database = helper.getWritableDatabase();

    String sql = "select * from " + DBHelper.TABLE_PATIENT;
    Cursor cursor = database.rawQuery(sql, null);
    if (cursor.moveToFirst()) {
       do {
           idList.add(cursor.getLong(0));
           list.add(cursor.getString(1));
           list.add(cursor.getString(2));
           list.add(cursor.getString(3));
           list.add(cursor.getString(4));
           list.add(cursor.getString(5));
           list.add(cursor.getString(6));
           list.add(cursor.getString(7));
           list.add(cursor.getString(8));
           list.add(cursor.getString(9));
           list.add(cursor.getString(10));
           list.add(cursor.getString(11));
           list.add(cursor.getString(12));
           list.add(cursor.getString(13));

       } while (cursor.moveToNext());
    }

    TV_Name.setText(cursor.getString(1)); 
        TV_DOB.setText(cursor.getString(2));
    cursor.close(); 

}

しかし、私はエラー java.lang.NullPointerException を取得しています

更新:ここに私のDB

public class DBHelper extends SQLiteOpenHelper{

public static final String DB_NAME = "MyDocDB";
public static final int DB_VERSION = 1; 

//Table Patient Details//
    public static final String TABLE_PATIENT = "patient";
    public static final String PATIENT_ID = "id";
    public static final String NAME = "name";
    public static final String DOB = "dob";
    public static final String ADDRESS = "address";
    public static final String BLOODTYPE = "bloodtype";
    public static final String HEIGHT = "height";
    public static final String DRNAME = "drname";
    public static final String DRCONTACT = "drcontact";
    public static final String GUARDIANNAME1 = "guardianname1";
    public static final String GUARDIANCONTACT1 = "guardiancontact1";
    public static final String GUARDIANNAME2 = "guardianname2";
    public static final String GUARDIANCONTACT2 = "guardiancontact2";
    public static final String HOSPITALNAME = "hospitalname";
    public static final String HOSPITALCONTACT = "hospitalcontact";

public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    createTable(db);    
            testInsert(db);
}

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

//create table
public void createTable(SQLiteDatabase db)
{
    String query = "create table "+ TABLE_PATIENT +" (" +
            PATIENT_ID + " integer primary key autoincrement," +
            NAME + " text," +
            DOB + " text," +
            ADDRESS + " text," +
            BLOODTYPE + " text," +
            HEIGHT + " text," +
            DRNAME + " text," +
            DRCONTACT + " text," +
            GUARDIANNAME1 + " text," +
            GUARDIANCONTACT1 + " text," +
            GUARDIANNAME2 + " text," +
            GUARDIANCONTACT2 + " text," +
            HOSPITALNAME + " text," +
            HOSPITALCONTACT + " text)";
    db.execSQL(query);  
}


//insert
public void insertDataProfile(SQLiteDatabase db, String name, String dob,
        String address, String bloodtype, String height,
        String drname, String drcontact, String guardianname1, 
        String guardiancontact1, String guardianname2, 
        String guardiancontact2, String hospitalname, String     hospitalcontact)
{
    ContentValues cvPatient = new ContentValues();
    cvPatient.put(NAME, name);
    cvPatient.put(DOB, dob);
    cvPatient.put(ADDRESS, address);
    cvPatient.put(BLOODTYPE, bloodtype);
    cvPatient.put(HEIGHT, height);
    cvPatient.put(DRNAME, drname);
    cvPatient.put(DRCONTACT, drcontact);
    cvPatient.put(GUARDIANNAME1, guardianname1);
    cvPatient.put(GUARDIANCONTACT1, guardiancontact1);
    cvPatient.put(GUARDIANNAME2, guardianname2);
    cvPatient.put(GUARDIANCONTACT2, guardiancontact2);
    cvPatient.put(HOSPITALNAME, hospitalname);
    cvPatient.put(HOSPITALCONTACT, hospitalcontact);
    db.insert(TABLE_PATIENT, null, cvPatient);
}

public void testInsert(SQLiteDatabase db)
{
    db.execSQL("INSERT INTO "
    + TABLE_PATIENT
    + "name, dob, address, bloodtype, height, drname, "
    + "drcontact, guardianname1, guardiancontact1, " 
    + "guardianname2, guardiancontact2,hospitalname, "
    + "hospitalcontact"
    + " VALUES ('muna', '11/02/90', 'No 34, Taman Seri Mengkuang 8', " +
    "'0', '158', 'dr nurul', '12344', 'dada', '141'," +
    "'fafa', '34141', 'faga', '14141');");

public Cursor getAllData(SQLiteDatabase db)
{

    return db.query(TABLE_PATIENT, new String[]{PATIENT_ID, NAME, ADDRESS,
            BLOODTYPE, HEIGHT, DRNAME, DRCONTACT, GUARDIANNAME1, GUARDIANCONTACT1,
            GUARDIANNAME2, GUARDIANCONTACT2, HOSPITALNAME, HOSPITALCONTACT},
            null, null, null, null, null);  
}

}
4

2 に答える 2

1

{{

 protected void onCreate(Bundle savedInstanceState) 
 {   
  super.onCreate(savedInstanceState);
  setContentView(R.layout.profile);

  TextView TV_Name = (TextView)findViewById(R.id.TVName);
  TextView TV_DOB = (TextView)findViewById(R.id.TVDOB);
  TextView TV_Address = (TextView)findViewById(R.id.TVAddress);
  TextView TV_BloodType = (TextView)findViewById(R.id.TVBloodType);
  TextView TV_Height =    (TextView)findViewById(R.id.TVHeight);
  TextView TV_DrName =    (TextView)findViewById(R.id.TVDrName);
  TextView TV_DrContact = (TextView)findViewById(R.id.TVDrContact);
  TextView TV_CaretakerName = (TextView)findViewById(R.id.TVCaretakerName);
  TextView TV_CaretakerContact = (TextView)findViewById(R.id.TVCaretakerContact);
  TextView TV_HospitalName = (TextView)findViewById(R.id.TVHospitalName);
  TextView TV_HospitalContact = (TextView)findViewById(R.id.TVHospitalContact);

DBHelper helper = new DBHelper(this);
database = helper.getWritableDatabase();

String sql = "select * from " + DBHelper.TABLE_PATIENT;
Cursor cursor = database.rawQuery(sql, null);
if (cursor.moveToFirst()) {
   do {
       idList.add(cursor.getLong(0));
       list.add(cursor.getString(1));
       list.add(cursor.getString(2));
       list.add(cursor.getString(3));
       list.add(cursor.getString(4));
       list.add(cursor.getString(5));
       list.add(cursor.getString(6));
       list.add(cursor.getString(7));
       list.add(cursor.getString(8));
       list.add(cursor.getString(9));
       list.add(cursor.getString(10));
       list.add(cursor.getString(11));
       list.add(cursor.getString(12));
       list.add(cursor.getString(13));

   } while (cursor.moveToNext());
}

cursor.moveToFirst();
TV_Name.setText(cursor.getString(1)); 
TV_DOB.setText(cursor.getString(2));
cursor.close(); 

}

これを試しましたか?

于 2013-02-22T05:25:20.020 に答える
0

これは、moveToNextを使用してカーソルを最後まで移動したためではありませんか?もう一度データにアクセスする前にMoveToFirstを呼び出してください。

于 2013-02-20T03:34:41.717 に答える