This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
去年关闭。
我一直在用从我的sqlite数据库中检索到的数据填充
我的数据库文件
错误日志:
在com.test.profilekeeper.LoadedData。(LoadedData.java:17)的位置:ArrayList listdata = dbhelper.getAll(); //构造函数
在com.test.profilekeeper.ProfileListActivity.onCreate(ProfileListActivity.java:23)处:LoadedData mydata = new LoadedData();
同样在
(12个答案)
去年关闭。
我一直在用从我的sqlite数据库中检索到的数据填充
ListView
,我的数据库文件
DBMainAdapter
中的代码可以完美地工作(片段以节省空间),我知道这一点,因为当我在一个正在使用的单独活动中调用我的getAll()
时(不包括在内),我得到了我的ArrayList
并且可以使用可以很好地打印出内部内容,但是我注意到,每当我尝试在ProfileListActivity
内部执行相同的操作时,都会不断抛出NullPointerException
。我究竟做错了什么?!错误日志:
04-21 06:42:31.669: W/dalvikvm(6107): threadid=1: thread exiting with uncaught exception (group=0xb3a8db90)
04-21 06:42:31.749: E/AndroidRuntime(6107): FATAL EXCEPTION: main
04-21 06:42:31.749: E/AndroidRuntime(6107): Process: com.test.profilekeeper, PID: 6107
04-21 06:42:31.749: E/AndroidRuntime(6107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.profilekeeper/com.test.profilekeeper.ProfileListActivity}: java.lang.NullPointerException
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.os.Handler.dispatchMessage(Handler.java:102)
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.os.Looper.loop(Looper.java:137)
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-21 06:42:31.749: E/AndroidRuntime(6107): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 06:42:31.749: E/AndroidRuntime(6107): at java.lang.reflect.Method.invoke(Method.java:515)
04-21 06:42:31.749: E/AndroidRuntime(6107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-21 06:42:31.749: E/AndroidRuntime(6107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-21 06:42:31.749: E/AndroidRuntime(6107): at dalvik.system.NativeStart.main(Native Method)
04-21 06:42:31.749: E/AndroidRuntime(6107): Caused by: java.lang.NullPointerException
04-21 06:42:31.749: E/AndroidRuntime(6107): at com.test.profilekeeper.LoadedData.<init>(LoadedData.java:17)
04-21 06:42:31.749: E/AndroidRuntime(6107): at com.test.profilekeeper.ProfileListActivity.onCreate(ProfileListActivity.java:23)
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.app.Activity.performCreate(Activity.java:5243)
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-21 06:42:31.749: E/AndroidRuntime(6107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
04-21 06:42:31.749: E/AndroidRuntime(6107): ... 11 more
在com.test.profilekeeper.LoadedData。(LoadedData.java:17)的位置:ArrayList listdata = dbhelper.getAll(); //构造函数
在com.test.profilekeeper.ProfileListActivity.onCreate(ProfileListActivity.java:23)处:LoadedData mydata = new LoadedData();
public class DBMainAdapter{
DBMain dbhelper;
public DBMainAdapter(Context context){
dbhelper = new DBMain(context);
}
public long insertprofiledata(String fname, String lname, String age, String gender, String username, String password){
SQLiteDatabase db = dbhelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBMain.FIRSTNAME, fname);
cv.put(DBMain.LASTNAME, lname);
cv.put(DBMain.AGE, age);
cv.put(DBMain.GENDER, gender);
cv.put(DBMain.USERNAME, username);
cv.put(DBMain.PASSWORD, password);
long id = db.insert(DBMain.TABLE_NAME, null, cv);
return id;
}
public ArrayList<String[]> getAll(){
SQLiteDatabase db = dbhelper.getWritableDatabase();
String[] columns = {DBMain.UID, DBMain.FIRSTNAME, DBMain.LASTNAME, DBMain.AGE, DBMain.GENDER};
Cursor curs = db.query(DBMain.TABLE_NAME, columns, null, null, null, null, null);
int index1 = curs.getColumnIndex(DBMain.UID);
int index2 = curs.getColumnIndex(DBMain.FIRSTNAME);
int index3 = curs.getColumnIndex(DBMain.LASTNAME);
int index4 = curs.getColumnIndex(DBMain.AGE);
int index5 = curs.getColumnIndex(DBMain.GENDER);
StringBuffer buffer = new StringBuffer();
while(curs.moveToNext()){
String uid = curs.getString(index1);
String fname = curs.getString(index2);
String lname = curs.getString(index3);
String age = curs.getString(index4);
String gender = curs.getString(index5);
buffer.append(uid+","+fname+","+lname+","+age+","+gender+" ");
}
String[] row = buffer.toString().split(" ");
String profilebuilder[][] = new String[row.length][row[0].length()];
for(int i = 0; i < row.length; i++){
String[] profile = row[i].split(",");
for(int j = 0; j < profile.length; j++){
profilebuilder[i][j] = profile[j];
}
}
String[] ids = new String[profilebuilder.length];
String[] firstname = new String[profilebuilder.length];
String[] ages = new String[profilebuilder.length];
String[] genders = new String[profilebuilder.length];
for(int k = 0; k < profilebuilder.length; k++){
for(int l = 0; l < profilebuilder[0].length; l++){
ids[k] = profilebuilder[k][0];
firstname[k] = profilebuilder[k][1];
ages[k] = profilebuilder[k][3];
genders[k] = profilebuilder[k][4];
}
}
ArrayList<String[]> alldata = new ArrayList<String[]>();
alldata.add(ids);
alldata.add(firstname);
alldata.add(ages);
alldata.add(genders);
return alldata;
}
}
public class LoadedData {
DBMainAdapter dbhelper;
String[] ids;
String[] firstnames;
String[] ages;
String[] genders;
public LoadedData(){
ArrayList<String[]> listdata = dbhelper.getAll();
ids = new String[listdata.get(0).length];
firstnames = new String[listdata.get(0).length];
ages = new String[listdata.get(0).length];
genders = new String[listdata.get(0).length];
ids = listdata.get(0);
firstnames = listdata.get(1);
ages = listdata.get(2);
genders = listdata.get(3);
}
public String[] getIds() {
return ids;
}
public String[] getFirstnames() {
return firstnames;
}
public String[] getAges() {
return ages;
}
public String[] getGenders() {
return genders;
}
}
public class ProfileListActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadedData mydata = new LoadedData();
populateListView(mydata);
}
private void populateListView(LoadedData mydata) {
ArrayAdapter<LoadedData> adapter = new MyListAdapter(mydata);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<LoadedData>{
LoadedData mdata;
public MyListAdapter(LoadedData data){
super(ProfileListActivity.this, R.layout.row);
this.mdata = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.row, parent, false);
}
TextView first_name = (TextView) itemView.findViewById(R.id.nameListTextView);
TextView id = (TextView) itemView.findViewById(R.id.idListTextView);
TextView age = (TextView) itemView.findViewById(R.id.ageListTextView);
ImageView img = (ImageView) itemView.findViewById(R.id.imageViewList);
id.setText(mdata.getIds()[position]);
first_name.setText(mdata.getFirstnames()[position]);
age.setText(mdata.getAges()[position]);
if(mdata.getGenders()[position].equalsIgnoreCase("Male")){
img.setImageResource(R.raw.man_thumb);
}else{
img.setImageResource(R.raw.woman_thumb);
}
return itemView;
}
}
}
最佳答案
您忘记了初始化LoadedData类中的dbhelper
对象。在下面的类中创建一个公共构造函数,
public DBMainAdapter(Context context)
{
dbhelper = new DBMain(context);
}
同样在
DBMainAdapter
类的getAll()方法中,在while循环之前编写以下语句,curs.moveToFirst();
10-06 09:28