-3
當我單擊「顯示」按鈕時,我的應用程序將崩潰。
我遵循指南,只是改變名稱,但由於某種原因,它不斷崩潰。當我嘗試獲取數據時,SQLite應用程序崩潰
請幫我理解爲什麼會發生這種情況。
這是我CustDbAdapter CODE:
public class CustDbAdapter {
public static final String CUST_TABLE_NAME = "customers";
public static final String _ID = "_id";
public static final String CUSTLNAME = "custlname";
public static final String CUSTFNAME = "custfname";
public static final String CUSTPHONE = "custphone";
private static final String DATABASE_NAME = "cool.db";
static final int CUSTBASE_VERSION = 1;
private static final String DATABASE_CREATE =
"CREATE TABLE " + CUST_TABLE_NAME + " ("
+ _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ CUSTLNAME + " TEXT NOT NULL, "
+ CUSTFNAME + " TEXT NOT NULL, "
+ CUSTPHONE + " INTEGER NOT NULL, "
private CustDbHelper dbhelper;
private final Context context;
private SQLiteDatabase db;
public CustDbAdapter(Context c) {
this.context = c;
dbhelper = new CustDbHelper(context);
}
private static class CustDbHelper extends SQLiteOpenHelper {
CustDbHelper(Context context){
super(context, DATABASE_NAME, null, CUSTBASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
try{
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
Log.w(TAG, "Upgrading database from version " + oldVersion + " to"
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXIST " + CUST_TABLE_NAME);
onCreate(db);
}
}
public CustDbAdapter open() throws SQLException {
dbhelper = new CustDbHelper(context);
db = dbhelper.getWritableDatabase();
return this;
}
public void close(){
dbhelper.close();
}
public long addCustomer(String lname, String fname, int phone) {
ContentValues initialValues = new ContentValues();
initialValues.put(CUSTLNAME, lname);
initialValues.put(CUSTFNAME, fname);
initialValues.put(CUSTPHONE, phone);
return db.insert(CUST_TABLE_NAME, null, initialValues);
}
public Cursor getAllCustomer(){
return db.query(CUST_TABLE_NAME, new String[]{_ID, CUSTLNAME,
CUSTFNAME, CUSTPHONE}, null, null, null, null, null);
}
}
,這是CustFragment調用Apapter。
public class CustFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_util_cust, container, false);
return v;
}
@Override
public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
final Button btnShow = (Button)v.findViewById(R.id.b_utilcust_input);
final TextView txtOutput = (TextView)v.findViewById(R.id.t_utilcust_output);
final CustDbAdapter db = new CustDbAdapter(getActivity());
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.open();
Cursor c = db.getAllCustomer();
if (c.moveToFirst()){
txtOutput.setText("Name - Phone \n");
c.moveToNext();
do {
DisplayContact(c);
} while (c.moveToNext());
}
db.close();
}
private void DisplayContact(Cursor c){
int idColumnIndex = c.getColumnIndex(CustDbAdapter._ID);
int lnameColumnIndex = c.getColumnIndex(CustDbAdapter.CUSTLNAME);
int fnameColumnIndex = c.getColumnIndex(CustDbAdapter.CUSTFNAME);
int phoneColumnIndex = c.getColumnIndex(CustDbAdapter.CUSTPHONE);
int currentID = c.getInt(idColumnIndex);
String currentLName = c.getString(lnameColumnIndex);
String currentFName = c.getString(fnameColumnIndex);
int currentPhone = c.getInt(phoneColumnIndex);
txtOutput.append("\n"
+ currentID + " - " + currentLName +", "+currentFName +" Phone:" + currentPhone);
}
});
}
}
分享你的崩潰日誌 –
連接錯誤日誌的問題 – akhilesh0707