我正在尝试创建数据库并对所述数据库执行一些CRUD操作。不知何故,当我尝试插入并选择时,会发生以下错误:
07-19 08:26:21.880: E/AndroidRuntime(2740): FATAL EXCEPTION: main
07-19 08:26:21.880: E/AndroidRuntime(2740): java.lang.ClassCastException: com.example.testdb.MainActivity$Select cannot be cast to java.lang.CharSequence
07-19 08:26:21.880: E/AndroidRuntime(2740): at com.example.testdb.MainActivity$2.onClick(MainActivity.java:47)
07-19 08:26:21.880: E/AndroidRuntime(2740): at android.view.View.performClick(View.java:4204)
07-19 08:26:21.880: E/AndroidRuntime(2740): at android.view.View$PerformClick.run(View.java:17355)
07-19 08:26:21.880: E/AndroidRuntime(2740): at android.os.Handler.handleCallback(Handler.java:725)
07-19 08:26:21.880: E/AndroidRuntime(2740): at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 08:26:21.880: E/AndroidRuntime(2740): at android.os.Looper.loop(Looper.java:137)
07-19 08:26:21.880: E/AndroidRuntime(2740): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-19 08:26:21.880: E/AndroidRuntime(2740): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 08:26:21.880: E/AndroidRuntime(2740): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 08:26:21.880: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-19 08:26:21.880: E/AndroidRuntime(2740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-19 08:26:21.880: E/AndroidRuntime(2740): at dalvik.system.NativeStart.main(Native Method)
所以我尝试创建一个单独的项目来测试我的代码。我有两个简单的按钮,一个用于插入记录,另一个用于显示在TextView
中检索的记录。插件工作找到,(我希望)然而,当我点击按钮检索数据时,显示上面的错误,应用程序崩溃。
以下是我的代码:
public class MainActivity extends Activity
{
static DatabaseImplementation di;
static SQLiteDatabase db;
Button btnInsert;
Button btnShow;
TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
di = new DatabaseImplementation(this);
db = di.getWritableDatabase();
btnInsert = (Button)findViewById(R.id.btnInsert);
btnInsert.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Insert().execute("");
}
});
btnShow = (Button)findViewById(R.id.btnShow);
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
AsyncTask<String, Void, String> result = new Select().execute("");
txtView = (TextView)findViewById(R.id.txtView);
txtView.setText((CharSequence) result);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private static class Insert extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
try
{
executeInsert();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
private static String executeInsert()
{
return "INSERT INTO tbl_Test VALUES(1, 'Testing');";
}
private static class Select extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
try
{
String result = executeSelect();
return result;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
private static String executeSelect()
{
return "SELECT test FROM tbl_Test WHERE _id = 1;";
}
}
这是我的.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnInsert"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Insert"/>
<Button
android:id="@+id/btnShow"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/btnInsert"
android:text="Show"/>
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnInsert"
android:text=""/>
</RelativeLayout>
我怀疑数据库没有被创建,但是如果是这样的话,那么只要我点击插入按钮,应用就会崩溃。知道问题可能是什么?
编辑:
对不起我忘了添加SQLIteOpenHelper类:
public class DatabaseImplementation extends SQLiteOpenHelper
{
private final String createTable = "CREATE TABLE tbl_Test (_id INTEGER PRIMARY KEY, test TEXT)";
public DatabaseImplementation(Context context)
{
super(context, "databaseName", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
@sirackh 07-19 09:32:05.580:E / AndroidRuntime(9498):致命异常:主要 07-19 09:32:05.580:E / AndroidRuntime(9498):java.lang.NullPointerException 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:43) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 07-19 09:32:05.580:E / AndroidRuntime(9498):at com.example.testdb.MainActivity $ Select.onPostExecute(MainActivity.java:115) 07-19 09:32:05.580:E / AndroidRuntime(9498):at com.example.testdb.MainActivity $ Select.onPostExecute(MainActivity.java:1) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.os.AsyncTask.finish(AsyncTask.java:631) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.os.AsyncTask.access $ 600(AsyncTask.java:177) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.os.Handler.dispatchMessage(Handler.java:99) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.os.Looper.loop(Looper.java:137) 07-19 09:32:05.580:E / AndroidRuntime(9498):在android.app.ActivityThread.main(ActivityThread.java:5041) 07-19 09:32:05.580:E / AndroidRuntime(9498):at java.lang.reflect.Method.invokeNative(Native Method) 07-19 09:32:05.580:E / AndroidRuntime(9498):at java.lang.reflect.Method.invoke(Method.java:511) 07-19 09:32:05.580:E / AndroidRuntime(9498):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 07-19 09:32:05.580:E / AndroidRuntime(9498):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-19 09:32:05.580:E / AndroidRuntime(9498):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
它在这一行给出错误:
txtView.setText((CharSequence) result);
因为它无法自动将AsyncTask
投射到CharSequence
。它应该是这样的:
txtView.setText(result.toString());
答案 1 :(得分:0)
在TextView上显示结果应该在AsyncTask类的postExecute上完成。所以实现这个方法并在那里设置值。
protected void onPostExecute(String result) {
txtView = (TextView)findViewById(R.id.txtView);
txtView.setText(result);
}