我按照此示例尝试:Link。然后我无法成功获取列表视图。 代码如下。请帮忙,谢谢。
package com.example.trydb3;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends Activity {
int i = 0;
private DBHelper dbhelper ;
private ListView listView1;
private SimpleCursorAdapter dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper = new DBHelper(this);
displayListView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void displayListView() {
Cursor cursor = dbhelper.fetchAllDatas();
// The desired columns to be bound
String[] columns = new String[] {
DBHelper.NAME,
DBHelper.TEL,
DBHelper.EMAIL
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.textView1,
R.id.textView2,
R.id.textView3
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.list_info,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
}
dbhelper,我认为fetchAllDatas()有一些问题,但我不知道为什么我可以成功创建和插入。
package com.example.trydb3;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
private static SQLiteDatabase db;
public static final String TABLE_NAME = "friends";
public static final String NAME = "name";
public static final String TEL = "tel";
public static final String EMAIL = "email";
public final static String DATABASE_NAME = "demo.db";
private final static int DATABASE_VERSION = 1;
private static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + "("
+ NAME + " TEXT,"
+ TEL + " TEXT,"
+ EMAIL + " TEXT"
+ ")";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
Log.d("Database operations","Database created successfully");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(DROP_TABLE);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
public void insertData(String name, String tel, String email){
SQLiteDatabase db= this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(NAME, name);
cv.put(TEL, tel);
cv.put(EMAIL, email);
long id = db.insert(TABLE_NAME, null, cv);
db.close();
}
public Cursor fetchAllDatas() {
String[] columns = new String[] {
NAME,
TEL,
EMAIL
};
Cursor mCursor = db.query(TABLE_NAME, columns,
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void insertSomeDatas() {
insertData("PAUL","12345678","123@fjkds.com");
insertData("JACK","987654321","345@fjkds.com");
insertData("TOMMY","44444444","567@fjkds.com");
}
}
然后它是main和listview的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="com.example.trydb3.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
</ListView>
</RelativeLayout>
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="26dp"
android:layout_toRightOf="@+id/textView2"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
日志文件:
11-15 15:56:43.536: E/Trace(32348): error opening trace file: No such file or directory (2)
11-15 15:56:44.186: D/AndroidRuntime(32348): Shutting down VM
11-15 15:56:44.186: W/dalvikvm(32348): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-15 15:56:44.196: E/AndroidRuntime(32348): FATAL EXCEPTION: main
11-15 15:56:44.196: E/AndroidRuntime(32348): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trydb3/com.example.trydb3.MainActivity}: java.lang.NullPointerException
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.os.Looper.loop(Looper.java:137)
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-15 15:56:44.196: E/AndroidRuntime(32348): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 15:56:44.196: E/AndroidRuntime(32348): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 15:56:44.196: E/AndroidRuntime(32348): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-15 15:56:44.196: E/AndroidRuntime(32348): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-15 15:56:44.196: E/AndroidRuntime(32348): at dalvik.system.NativeStart.main(Native Method)
11-15 15:56:44.196: E/AndroidRuntime(32348): Caused by: java.lang.NullPointerException
11-15 15:56:44.196: E/AndroidRuntime(32348): at com.example.trydb3.DBHelper.fetchAllDatas(DBHelper.java:58)
11-15 15:56:44.196: E/AndroidRuntime(32348): at com.example.trydb3.MainActivity.displayListView(MainActivity.java:49)
11-15 15:56:44.196: E/AndroidRuntime(32348): at com.example.trydb3.MainActivity.onCreate(MainActivity.java:26)
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.app.Activity.performCreate(Activity.java:5008)
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-15 15:56:44.196: E/AndroidRuntime(32348): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-15 15:56:44.196: E/AndroidRuntime(32348): ... 11 more
答案 0 :(得分:0)
在您的DBHelper课程中,您永远不会向您db
字段分配任何内容。
一个快速的工作 - 可能会在您的方法中获取数据库:
public Cursor fetchAllDatas() {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = new String[] {
NAME,
TEL,
EMAIL
};
Cursor mCursor = db.query(TABLE_NAME, columns,
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}