我正在尝试从sqlite表获取数据到视图列表,但是当我尝试连接到数据库时,我有一个错误: 05-02 13:59:18.123:E / AndroidRuntime(714):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.add / admed.Listar}:java.lang.NullPointerException
我的db连接器的代码是:
private static final String DB_NAME = "admed";
private SQLiteDatabase database;
private Basedados basedados;
public BasedadosConect(Context context) {
basedados = new Basedados(context, DB_NAME, null, 1);
}
public void open() throws SQLException
{
//open database in reading/writing mode
database = basedados.getWritableDatabase();
}
public boolean isOpen(){
if (database!=null){
return true;
} else{
return false;
}
}
public void close()
{
if (database != null)
database.close();
}
我要打印数据的类的代码(Listar.java):
BasedadosConect db;
protected void onCreate(Bundle savedInstanceState) {
Intent menu = getIntent();
String tipo = menu.getStringExtra("tipo");
super.onCreate(savedInstanceState);
if (tipo.equals("medicamentos")) {
setContentView(R.layout.medicamentos);
db.open();
db.insertMed(1,"trolax","trolax",2);
db.insertMed(2,"trolex","trolex",3);
Cursor cursor = db.getAllmeds();
String[] values = new String[cursor.getCount()];
if (cursor.moveToFirst())
{
for (int i = 0; i <= cursor.getCount(); i++)
{
values[i] = cursor.getString(2);
cursor.moveToNext();
}
}
db.close();
final ListView listview = (ListView) findViewById(R.id.listView1);
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
Button medicamentos = (Button) findViewById(R.id.button1);
medicamentos.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent medsIntent = new Intent(view.getContext(), Editar.class);
medsIntent.putExtra("tipo", "medicamentos");
medsIntent.putExtra("accao", "Adicionar");
startActivity(medsIntent);
}
});
}
Button voltar = (Button) findViewById(R.id.button2);
voltar.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.string.app_name, menu);
return true;
}
getAllmeds()代码:
public Cursor getAllmeds()
{
return database.query("medicamentos", new String[] {"medicamentos_desig"}, null, null, null, null, null);
}
完整日志:
05-02 13:59:18.113: W/dalvikvm(714): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-02 13:59:18.123: E/AndroidRuntime(714): FATAL EXCEPTION: main
05-02 13:59:18.123: E/AndroidRuntime(714): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.add/admed.Listar}: java.lang.NullPointerException
05-02 13:59:18.123: E/AndroidRuntime(714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-02 13:59:18.123: E/AndroidRuntime(714): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-02 13:59:18.123: E/AndroidRuntime(714): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-02 13:59:18.123: E/AndroidRuntime(714): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-02 13:59:18.123: E/AndroidRuntime(714): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 13:59:18.123: E/AndroidRuntime(714): at android.os.Looper.loop(Looper.java:123)
05-02 13:59:18.123: E/AndroidRuntime(714): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-02 13:59:18.123: E/AndroidRuntime(714): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 13:59:18.123: E/AndroidRuntime(714): at java.lang.reflect.Method.invoke(Method.java:507)
05-02 13:59:18.123: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-02 13:59:18.123: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-02 13:59:18.123: E/AndroidRuntime(714): at dalvik.system.NativeStart.main(Native Method)
05-02 13:59:18.123: E/AndroidRuntime(714): Caused by: java.lang.NullPointerException
05-02 13:59:18.123: E/AndroidRuntime(714): at admed.Listar.onCreate(Listar.java:34)
05-02 13:59:18.123: E/AndroidRuntime(714): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 13:59:18.123: E/AndroidRuntime(714): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-02 13:59:18.123: E/AndroidRuntime(714): ... 11 more
我做错了什么?提前谢谢!
答案 0 :(得分:1)
您正在调用另一个类的非静态方法。因此,您需要在调用该类的成员函数之前初始化类对象。仅凭对象声明还不够。
在db.open()之前插入此行;在Listar.Java的onCreate()中。
db = new BasedadosConect(this);