我的数据库表有数据,可以在sqlite数据库浏览器中查看。在下面的类中出现错误...如果有异常,它必须在catch循环中捕获,但仍然得到异常不知道它是如何发生的,请帮助我解决....并评论是否需要任何信息提前感谢
DataBaseView.java
package com.vibrator;
import android.app.Activity;
import java.util.ArrayList;
//import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class DataBaseView extends Activity{
DataBaseAdapter DataBaseAdapter;
ArrayList<String> al;
Cursor cursor;
public SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.database_view);
System.out.println("entered in database");
ListView list =(ListView) findViewById(R.id.list);
DataBaseAdapter=new DataBaseAdapter(this);
DataBaseAdapter=DataBaseAdapter.open();
try {
cursor=db.rawQuery("select * from DETAILS", null);
if(cursor!=null){
if (cursor.moveToFirst()) {
do {
String id =cursor.getString(cursor.getColumnIndex("ID"));
String name =cursor.getString(cursor.getColumnIndex("NAME"));
//String pattern =cursor.getString(cursor.getColumnIndex("PATTERN"));
//store the values in arraylist
al.add(id+". "+name);
} while (cursor.moveToNext());
}
}
DataBaseAdapter.close();
}
catch (Exception e) {
Toast.makeText(this, "exception occurred", Toast.LENGTH_LONG).show();
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, al);
list.setAdapter(adapter);
}
}
logcat的:
08-05 20:25:10.988: E/AndroidRuntime(9500): FATAL EXCEPTION: main
08-05 20:25:10.988: E/AndroidRuntime(9500): java.lang.NullPointerException
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.widget.ListView.onMeasure(ListView.java:1081)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.view.View.measure(View.java:8171)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.view.View.measure(View.java:8171)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.view.View.measure(View.java:8171)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.view.View.measure(View.java:8171)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.os.Looper.loop(Looper.java:123)
08-05 20:25:10.988: E/AndroidRuntime(9500): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-05 20:25:10.988: E/AndroidRuntime(9500): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 20:25:10.988: E/AndroidRuntime(9500): at java.lang.reflect.Method.invoke(Method.java:521)
08-05 20:25:10.988: E/AndroidRuntime(9500): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-05 20:25:10.988: E/AndroidRuntime(9500): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-05 20:25:10.988: E/AndroidRuntime(9500): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
当你声明但是忘记初始化一个对象/变量时,你通常会得到 NullPointerException 。
al = new ArrayList<String>();
您忘记初始化 al 对象
答案 1 :(得分:0)
您永远不会初始化al
。您在上面定义它,但从不创建实际对象。在数据库查询中向onCreate()
添加任何内容之前,请在al
方法中包含此内容:
al = new ArrayList<String>();