当我在模拟器中运行程序无法正常工作并显示这些错误时。这是在Android Studio版本3.5中,我在代码无法正常运行的应用程序中遇到问题,我尝试在模拟器和外部设备中运行该程序,并且它没用
package com.example.listes;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase mDatabase;
private GroceryAdapter mAdapter;
private EditText mEdiText;
private TextView mTextV;
private int mAmount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GroceryDBHelper dbHelper = new GroceryDBHelper(this);
mDatabase = dbHelper.getWritableDatabase();
RecyclerView recyclerView = findViewById(R.id.RecyVi);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new GroceryAdapter(this, getAllItems());
recyclerView.setAdapter(mAdapter);
mEdiText = findViewById(R.id.Edtxt);
mTextV = findViewById(R.id.TextV);
Button butM = findViewById(R.id.butM);
Button butB = findViewById(R.id.butB);
Button butADD = findViewById(R.id.butADD);
butM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
increase();
}
});
butB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
decrease();
}
});
butADD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addItem();
}
});
}
private void increase(){
mAmount++;
mTextV.setText(String.valueOf(mAmount));
}
private void decrease(){
if (mAmount > 0) {
mAmount--;
mTextV.setText(String.valueOf(mAmount));
}
}
private void addItem(){
if (mEdiText.getText().toString().trim().length() == 0 || mAmount == 0){
return;
}
String name = mEdiText.getText().toString();
ContentValues cv = new ContentValues();
cv.put(GroceryCon.GroceryEntry.COLUMN_NAME, name);
cv.put(GroceryCon.GroceryEntry.COLMUN_AMOUNT, mAmount);
mDatabase.insert(GroceryCon.GroceryEntry.TABLE_NAME, null, cv);
mAdapter.swapCursor(getAllItems());
mEdiText.getText().clear();
}
private Cursor getAllItems(){
return mDatabase.query(
GroceryCon.GroceryEntry.TABLE_NAME,
null,
null,
null,
null,
null,
GroceryCon.GroceryEntry.COLMUN_TIMESTAMP + " DESC"
);
}
}
2.GroceryCon
package com.example.listes;
import android.provider.BaseColumns;
public class GroceryCon {
private GroceryCon(){}
public static final class GroceryEntry implements BaseColumns{
public static final String TABLE_NAME = "groceryList";
public static final String COLUMN_NAME = "name";
public static final String COLMUN_AMOUNT = "amount";
public static final String COLMUN_TIMESTAMP ="timestamp";
}
}
3.GroceryDBHelper
package com.example.listes;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.example.listes.GroceryCon.GroceryEntry;
public class GroceryDBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "grocerylist.db";
public static final int DATABASE_VERSION =1;
public GroceryDBHelper(@Nullable Context context) {
super(context,DATABASE_NAME , null,DATABASE_VERSION );
}
@Override
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE_GROCERYLIST_TABLE ="CREATE TABLE " +
GroceryEntry.TABLE_NAME + " (" +
GroceryEntry._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
GroceryEntry.COLUMN_NAME + " TEXT NOT NULL," +
GroceryEntry.COLMUN_AMOUNT + "INTEGER NOT NULL," +
GroceryEntry.COLMUN_TIMESTAMP + "TIMESTAMP DEFAULT CURRENT_TIMESTAMP" +
" );";
db.execSQL(SQL_CREATE_GROCERYLIST_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + GroceryEntry.TABLE_NAME);
}
}
4.GroceryAdapter
package com.example.listes;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class GroceryAdapter extends RecyclerView.Adapter<GroceryAdapter.GroceryViewHolder> {
private Context mContext;
private Cursor mCursor;
public GroceryAdapter(Context context , Cursor cursor){
mContext = context;
mCursor = cursor;
}
public class GroceryViewHolder extends RecyclerView.ViewHolder{
public TextView nameText;
public TextView countText;
public GroceryViewHolder(@NonNull View itemView) {
super(itemView);
nameText = itemView.findViewById(R.id.textItem);
countText = itemView.findViewById(R.id.textGro);
}
}
@NonNull
@Override
public GroceryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.groc_layout, parent, false);
return new GroceryViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull GroceryViewHolder holder, int position) {
if (!mCursor.moveToPosition(position)){
return;
}
String name = mCursor.getString(mCursor.getColumnIndex(GroceryCon.GroceryEntry.COLUMN_NAME));
int amount = mCursor.getInt(mCursor.getColumnIndex(GroceryCon.GroceryEntry.COLMUN_AMOUNT));
holder.nameText.setText(name);
holder.countText.setText(String.valueOf(amount));
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public void swapCursor(Cursor newCursor){
if (mCursor != null){
mCursor.close();
}
mCursor = newCursor;
if (newCursor != null){
notifyDataSetChanged();
}
}
}
E/SQLiteLog: (1)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.listes, PID: 20952
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listes/com.example.listes.MainActivity}: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (Sqlite code 1): , while compiling: CREATE TABLE groceryList (_idINTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL,amountINTEGER NOT NULL,timestampTIMESTAMP DEFAULT CURRENT_TIMESTAMP );, (OS error - 2:No such file or directory)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (Sqlite code 1): , while compiling: CREATE TABLE groceryList (_idINTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL,amountINTEGER NOT NULL,timestampTIMESTAMP DEFAULT CURRENT_TIMESTAMP );, (OS error - 2:No such file or directory)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:925)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:536)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:603)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:63)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1968)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1892)
at com.example.listes.GroceryDBHelper.onCreate(GroceryDBHelper.java:30)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:310)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:206)
at com.example.listes.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:7383)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
I/Process: Sending signal. PID: 20952 SIG: 9
Process 20952 terminated.
这是在Android Studio 3.5版中,我在代码无法正常运行的应用程序中遇到问题,我尝试在模拟器和外部设备中运行它,但它不起作用