我是android新手。 当我插入数据时,它可以正常工作。但是,当我单击用于使用recyclerview显示插入的数据的按钮时,会出现以下错误消息:Sqlite插入和视图已停止。
错误消息如下:
MainActivity代码:
public class MainActivity extends AppCompatActivity {
private EditText nameEt, ageEt;
private Button insertBtn;
String name, age;
DatabaseHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
insertData();
}
private void insertData() {
insertBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = nameEt.getText().toString();
age = ageEt.getText().toString();
long id = helper.insertData(name, age);
Toast.makeText(MainActivity.this, "Your id" + id, Toast.LENGTH_SHORT).show();
}
});
}
private void init() {
nameEt = findViewById(R.id.nameEt);
ageEt = findViewById(R.id.ageEt);
insertBtn = findViewById(R.id.insertBtn);
helper = new DatabaseHelper(this);
}
public void ShowData(View view) {
startActivity(new Intent(this, ShowActivity.class));
}
}
ShowActivity代码:
public class ShowActivity extends AppCompatActivity {
RecyclerView recyclerView;
UserAdapter adapter;
List<User> users;
DatabaseHelper helper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
init();
getData();
}
private void getData(){
Cursor cursor = helper.showData();
while (cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex(helper.COL_ID));
String name = cursor.getString(cursor.getColumnIndex(helper.COL_NAME));
String age = cursor.getString(cursor.getColumnIndex(helper.COL_AGE));
users.add(new User(id,name,age));
adapter.notifyDataSetChanged();
}
}
private void init(){
recyclerView = findViewById(R.id.userRecyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
users = new ArrayList<>();
helper = new DatabaseHelper(this);
adapter = new UserAdapter(users);
recyclerView.setAdapter(adapter);
}
DatabaseHelper代码:
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DATABASE_NAME = "User.db";
private static String TABLE_NAME = "User";
public static String COL_ID = "Id";
public static String COL_NAME = "Name";
public static String COL_AGE = "Age";
private static int VERSION = 1;
private static String createTable = "create table "+TABLE_NAME+"(Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age TEXT)";
public DatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
long insertData(String name, String age){
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME,name);
contentValues.put(COL_AGE,age);
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
long id = sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
sqLiteDatabase.close();
return id;
}
public Cursor showData(){
String show_all = " select* From "+TABLE_NAME;
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(show_all,null);
return cursor;
}
}
答案 0 :(得分:0)
您没有在ShowActivity
文件中添加AndroidManifest.xml
类。将此添加到application
标记内
<activity android:name=".ShowActivity"/>