我花了一天时间试图理解这一点(不用说我在学习曲线上走了很长一段路 - 所以我需要求助!) - 下面是一个简单的数据库程序(我正在尝试了解他们如何工作到我正在处理的另一个应用程序中。)
当我点击“显示”按钮时 - 我收到以下运行时错误:
Log Cat
03-14 16:13:39.612: ERROR/AndroidRuntime(341): FATAL EXCEPTION: main
03-14 16:13:39.612: ERROR/AndroidRuntime(341): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.dbtest/com.android.dbtest.ShowActivity}: java.lang.NullPointerException
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.os.Looper.loop(Looper.java:123)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.app.ActivityThread.main(ActivityThread.java:3647)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at java.lang.reflect.Method.invoke(Method.java:507)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at dalvik.system.NativeStart.main(Native Method)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): Caused by: java.lang.NullPointerException
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.app.Activity.findViewById(Activity.java:1647)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at com.android.dbtest.ShowActivity.<init>(ShowActivity.java:12)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at java.lang.Class.newInstanceImpl(Native Method)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at java.lang.Class.newInstance(Class.java:1409)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
03-14 16:13:39.612: ERROR/AndroidRuntime(341): ... 11 more
我的主要活动:
package com.android.dbtest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class DatabaseTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void handleClick(View v){
Intent intent = new Intent();
intent.setClass(this, AddActivity.class);
startActivity(intent);
}
public void showHandleClick(View v){
Intent intent = new Intent();
intent.setClass(this, ShowActivity.class);
startActivity(intent);
}
}
我的显示活动(这是我认为主要错误(猜测不止一个)可能存在的地方,因为当我按下显示按钮时,发生运行时错误,我相信它与构建列表有关) :
package com.android.dbtest;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.util.Log;
public class ShowActivity extends DatabaseTestActivity{
private SQLiteAdapter mySQLiteAdapter;
ListView listContent = (ListView)findViewById(R.id.contentlist);
//TextView listContent = (TextView) findViewById(R.id.contentlist);
SimpleCursorAdapter cursorAdapter;
String cursor;
public static final String LOG_TAG = "dbtest";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
// TODO Auto-generated method stub
Log.v(LOG_TAG, "step 1");
//output.setText(String.valueOf( WHAT I GET FROM DATABASE));
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{SQLiteAdapter.KEY_CONTENT};
int[] to = new int[] {R.id.text};
Log.v(LOG_TAG, "from value:" + from );
Log.v(LOG_TAG, "to value:" + to );
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
}
}
我的SQLiteAdapter:
package com.android.dbtest;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_CONTENT = "Content";
public static final String KEY_ID = "_id"; //primary key
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_CONTENT + " text not null " +
");";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c)
{
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException
{
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase(); return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null, null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper
{
public SQLiteHelper(Context context, String name, CursorFactory factory, int version)
{
super(context, name, factory, version);
}
@Override public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub } } }
}
}
}
最后是Show:
的XML文件<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:id="@+id/textView1"></TextView>
<ListView android:id="@android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
答案 0 :(得分:2)
您不能将此作为活动中的静态初始化程序:
ListView listContent = (ListView)findViewById(R.id.contentlist);
原因是你还没有为你的活动加载布局,因此这个调用将失败findViewById(R.id.contentlist)
。您的错误来自类静态初始值设定项,因为您没有声明类的构造函数,仍会在<init>.
修复:致电onCreate
后,在setContentView(R.layout.show);
初始化列表视图。
答案 1 :(得分:1)
您的ShowActivity
活动的布局包含ListView
,其ID为@android:id/list
,但您搜索ID为R.id.contentlist
的listView。此外,您必须在设置布局后在onCreate()
方法中初始化视图。