我试图用SQLite数据库创建一个应用程序。我在本教程https://www.youtube.com/watch?v=d_PjDM4nFxI中做了所有内容,但我将“玩家”更改为“书籍”,并没有添加“位置”。应用程序打开,但在按下“检索”按钮时崩溃(“不幸的是,库已停止”)。也许有人可以帮助我?非常感谢!
错误:
04-28 16:16:27.889 2326-2326/com.example.kompiuteris.library E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kompiuteris.library, PID: 2326
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:312)
at android.content.res.Resources.getString(Resources.java:400)
at android.content.Context.getString(Context.java:409)
at com.example.kompiuteris.library.MainActivity$2.onClick(MainActivity.java:72)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
DBAdapter.java
static final String ROW_ID ="id";
static final String NAME ="name";
static final String TAG = "DBAdapter";
static final String DBNAME="m_DB";
static final String TBNAME="m_TB";
static final int DBVERSION='1';
static final String CREATE_TB="CREATE TABLE m_TB(id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "name TEXT NOT NULL);";
final Context c;
SQLiteDatabase db;
DBHelper helper;
public DBAdapter(Context ctx) {
this.c = ctx;
helper = new DBHelper(c);
}
private static class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TB);
} catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("DBAdapter","Upgrading DB");
db.execSQL("DROP TABLE IF EXISTS m_TB");
onCreate(db);
}
}
public DBAdapter openDB()
{
try {
db=helper.getWritableDatabase();
} catch (SQLException e)
{
Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
}
return this;
}
public void close()
{
helper.close();
}
public long add(String name)
{
try {
ContentValues cv = new ContentValues();
cv.put(NAME,name);
return db.insert(TBNAME,ROW_ID,cv);
} catch (SQLException e)
{
e.printStackTrace();
}
return 0;
}
public Cursor getAllNames()
{
String[] columns={ROW_ID,NAME};
return db.query(TBNAME,columns,null,null,null,null,null);
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView lv;
EditText nameTxt;
Button savebtn,retrievebtn;
ArrayList<String> books = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameTxt=(EditText)findViewById(R.id.editText);
savebtn=(Button)findViewById(R.id.saveBtn);
retrievebtn=(Button)findViewById(R.id.retrieveBtn);
lv = (ListView)findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_selectable_list_item,books);
final DBAdapter db = new DBAdapter(this);
savebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.openDB();
long result = db.add(nameTxt.getText().toString());
if(result > 0)
{
nameTxt.setText("");
}else
{
Toast.makeText(getApplicationContext(),"Failure", Toast.LENGTH_SHORT).show();
}
db.close();
}
});
retrievebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
books.clear();
db.openDB();
Cursor c = db.getAllNames();
while (c.moveToNext())
{
String name = getString(1);
books.add(name);
}
lv.setAdapter(adapter);
db.close();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),books.get(position), Toast.LENGTH_SHORT).show();
}
});
}
答案 0 :(得分:1)
问题是getString(1)
,您应该使用string
资源中的有效ID,因为您使用getString
的{{1}}方法。
您应该使用activity