2017-10-09 4 views
-3

[表示]ボタンをクリックすると、アプリがクラッシュします。
私はガイドに従っていて、ここでは名前を変更するだけですが、何らかの理由でクラッシュしてしまいます。データを取得しようとするとSQLiteアプリがクラッシュする

なぜこれが起こっているのか理解してください。

これは私のCustDbAdapterコードです:

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); 
} 

} 

、これはApapterを呼び出すCustFragmentです。

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); 

      } 
     }); 
    } 
} 
+2

は、あなたのクラッシュログ –

+0

を共有する必要がある質問とエラーログを添付 – akhilesh0707

答えて

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 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)" 
関連する問題