android listview运行时报异常
如图为运行时报错。源码如下:packagecom.example.canvassql;importjava.util.ArrayList;importjava.util....
如图为运行时报错。源码如下:
package com.example.canvassql;
import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;import com.DAO.*;
public class ListActivity extends Activity { SQLiteDatabase db; TempNumDAO mTempNumDAO; SimpleAdapter listAdapter; ArrayList<HashMap<String, Object>> listData; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.parant); try{ listData = mTempNumDAO.getAllData(); }catch(Exception e){ e.printStackTrace(); } ListView list = (ListView)findViewById(R.id.list_items); listAdapter = new SimpleAdapter(ListActivity.this, listData, R.layout.item, new String[]{"id","tnum","datetime"}, new int[]{R.id.id,R.id.tempnum,R.id.time}); list.setAdapter(listAdapter); } }
第31行代码是:
list.setAdapter(listAdapter); 展开
package com.example.canvassql;
import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;import com.DAO.*;
public class ListActivity extends Activity { SQLiteDatabase db; TempNumDAO mTempNumDAO; SimpleAdapter listAdapter; ArrayList<HashMap<String, Object>> listData; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.parant); try{ listData = mTempNumDAO.getAllData(); }catch(Exception e){ e.printStackTrace(); } ListView list = (ListView)findViewById(R.id.list_items); listAdapter = new SimpleAdapter(ListActivity.this, listData, R.layout.item, new String[]{"id","tnum","datetime"}, new int[]{R.id.id,R.id.tempnum,R.id.time}); list.setAdapter(listAdapter); } }
第31行代码是:
list.setAdapter(listAdapter); 展开
1个回答
展开全部
首先:
try{
listData = mTempNumDAO.getAllData();
}catch(Exception e){
e.printStackTrace();
}
这里mTempNumDAO没有初始化就是用,必定空指针异常,结果就导致 listData赋值失败,之后 listData依旧为null,
这时候
listAdapter = new SimpleAdapter(ListActivity.this, listData, R.layout.item,
new String[]{"id","tnum","datetime"},
是用空指针listData对listAdapter初始化,当然会出现空指针异常了
你至少应该mTempNumDAO = new TempNumDAO();一下吧,亲~
try{
listData = mTempNumDAO.getAllData();
}catch(Exception e){
e.printStackTrace();
}
这里mTempNumDAO没有初始化就是用,必定空指针异常,结果就导致 listData赋值失败,之后 listData依旧为null,
这时候
listAdapter = new SimpleAdapter(ListActivity.this, listData, R.layout.item,
new String[]{"id","tnum","datetime"},
是用空指针listData对listAdapter初始化,当然会出现空指针异常了
你至少应该mTempNumDAO = new TempNumDAO();一下吧,亲~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询