我从SQlite
db获取数据并将其存储在array list
中并在列表视图中填充数据但我在null pointer exception
中获得logcat
并且应用程序强制关闭,我不明白错误是什么,请有人指导我,我犯了错误。
这是我的活动类代码:
package testing.testapp;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class Administrator extends Activity {
List<User> usersList;
SimpleCursorAdapter cursorAdapter;
List<User> userList;
TextView fullName;
TextView cellNumber;
ListView list;
AdministratorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.administrator);
list=(ListView)findViewById(R.id.fullname);
DatabaseHandler h = new DatabaseHandler(this);
usersList = h.getAllUsers();
adapter = new AdministratorAdapter(this, R.layout.row, userList);
list.setAdapter(adapter);
}
public class AdministratorAdapter extends ArrayAdapter<User>
{
Context context;
Administrator holder;
int layoutResourceId;
List<User> userList;
public AdministratorAdapter(Context context, int layoutResourceId,List<User> userList)
{
super(context, layoutResourceId, userList);
this.context=context;
this.layoutResourceId=layoutResourceId;
this.userList=userList;
}
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
View row=convertView;
if(row==null)
{
LayoutInflater inflater=((Activity)context).getLayoutInflater();
row=inflater.inflate(layoutResourceId, parent,false);
holder=new Administrator();
holder.fullName=(TextView)row.findViewById(R.id.fulname);
holder.cellNumber=(TextView)row.findViewById(R.id.mobile);
row.setTag(holder);
}
else
{
holder=(Administrator)row.getTag();
}
holder.fullName.setText(userList.get(position).getUserName());
holder.cellNumber.setText(userList.get(position).getPassword());
return row;
}
这是我的数据库处理类:
package testing.testapp;
import java.util.ArrayList;
import java.util.List;
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 DatabaseHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 4;
public static final String DATABASE_NAME = "usersManager";
public static final String TABLE_USERS = "users";
public static final String KEY_USER_ID = "_id";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_ROLE = "role";
public static final String KEY_GPA = "gpa";
public static final String KEY_FIRST_NAME = "firstname";
public static final String KEY_LAST_NAME = "lastname";
public static final String KEY_GENDER = "gender";
public static final String KEY_CELL_NUMBER = "cellnumber";
public DatabaseHandler(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS + "("
+ KEY_USER_ID + " INTEGER PRIMARY KEY," + KEY_USERNAME + " TEXT,"
+ KEY_PASSWORD +" TEXT," + KEY_ROLE + " TEXT," + KEY_GPA + " TEXT,"
+ KEY_FIRST_NAME + " TEXT," + KEY_LAST_NAME + " TEXT,"
+ KEY_GENDER + " TEXT," + KEY_CELL_NUMBER + " TEXT" + ")";
// String CREATE_USERS_TABLE = "create table users ("
// + "_id integer primary key autoincrement, "
// + "username string not null, "
// + "password string not null, "
// + ");";
db.execSQL(CREATE_USERS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
onCreate(db);
}
public void addUser(User user)
{
SQLiteDatabase db = this.getWritableDatabase();
int f =db.getVersion();
Log.d("version", Integer.toString(f));
ContentValues values = new ContentValues();
values.put(KEY_USERNAME,user.getUserName());
values.put(KEY_PASSWORD, user.getPassword());
values.put(KEY_ROLE, user.getRole());
db.insert(TABLE_USERS, null, values);
db.close();
}
public int Login(String username,String password,String role)
{
try
{
SQLiteDatabase db = this.getWritableDatabase();
int i = 0;
Cursor c = null;
String query = "SELECT * FROM " + TABLE_USERS + " WHERE " + KEY_USERNAME+ "='" + username +"'" + " AND " +KEY_PASSWORD+ "='"+password+"'" + " AND " +KEY_ROLE+ "='"+role+"'" ;
//String query = "SELECT * FROM users WHERE username = '"+username+"' AND password = '"+password+"'";
//String query = "SELECT * FROM USERS";
c = db.rawQuery(query, null);
c.moveToFirst();
i = c.getCount();
c.close();
db.close();
return i;
}
catch(Exception e)
{
e.printStackTrace();
}
return 0;
}
public void AddInfo(User user)
{
SQLiteDatabase db = this.getWritableDatabase();
int f =db.getVersion();
Log.d("version", Integer.toString(f));
ContentValues values = new ContentValues();
values.put(KEY_FIRST_NAME,user.getFirstName());
values.put(KEY_LAST_NAME, user.getLastName());
values.put(KEY_GENDER, user.getGender());
values.put(KEY_GPA,user.getGpa());
values.put(KEY_CELL_NUMBER, user.getCellNumber());
db.insert(TABLE_USERS, null, values);
db.close();
}
public List<User> getAllUsers()
{
List<User> usersList = new ArrayList<User>();
String query = "SELECT * FROM " + TABLE_USERS ;
//+ " WHERE " + KEY_ROLE+ "=Student"
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
//return cursor;
if(cursor.moveToFirst())
{
do
{
User user = new User();
user.setUserId(Integer.parseInt(cursor.getString(0)));
user.setUserName(cursor.getString(1));
user.setPassword(cursor.getString(2));
user.setRole(cursor.getString(3));
user.setGpa(cursor.getString(4));
user.setFirstName(cursor.getString(5));
user.setLastName(cursor.getString(6));
user.setGender(cursor.getString(7));
user.setCellNumber(cursor.getString(8));
usersList.add(user);
}while(cursor.moveToNext());
}
db.close();
return usersList;
}
public User getUserDetail(String username)
{
SQLiteDatabase db = this.getWritableDatabase();
//String query = "SELECT " + KEY_USERNAME+" , " + KEY_PASSWORD + " FROM " + TABLE_USERS + " WHERE " + KEY_USERNAME + " = '" + username + "' ";
String query = "SELECT * FROM " + TABLE_USERS;
Cursor cursor = db.rawQuery(query, null);
User user = new User();
if(cursor!=null)
{
user.setUserId(Integer.parseInt(cursor.getString(0)));
user.setUserName(cursor.getString(1));
user.setPassword(cursor.getString(2));
}
return user;
}
}
}
}
这是我的logcat:
12-06 11:05:47.651: E/AndroidRuntime(634): FATAL EXCEPTION: main
12-06 11:05:47.651: E/AndroidRuntime(634): java.lang.RuntimeException: Unable to start activity ComponentInfo{testing.testapp/testing.testapp.Administrator}: java.lang.NullPointerException
12-06 11:05:47.651: E/AndroidRuntime(634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-06 11:05:47.651: E/AndroidRuntime(634): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-06 11:05:47.651: E/AndroidRuntime(634): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-06 11:05:47.651: E/AndroidRuntime(634): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-06 11:05:47.651: E/AndroidRuntime(634): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 11:05:47.651: E/AndroidRuntime(634): at android.os.Looper.loop(Looper.java:123)
12-06 11:05:47.651: E/AndroidRuntime(634): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-06 11:05:47.651: E/AndroidRuntime(634): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 11:05:47.651: E/AndroidRuntime(634): at java.lang.reflect.Method.invoke(Method.java:507)
12-06 11:05:47.651: E/AndroidRuntime(634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 11:05:47.651: E/AndroidRuntime(634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 11:05:47.651: E/AndroidRuntime(634): at dalvik.system.NativeStart.main(Native Method)
12-06 11:05:47.651: E/AndroidRuntime(634): Caused by: java.lang.NullPointerException
12-06 11:05:47.651: E/AndroidRuntime(634): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
12-06 11:05:47.651: E/AndroidRuntime(634): at android.widget.ListView.setAdapter(ListView.java:454)
12-06 11:05:47.651: E/AndroidRuntime(634): at testing.testapp.Administrator.onCreate(Administrator.java:37)
12-06 11:05:47.651: E/AndroidRuntime(634): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-06 11:05:47.651: E/AndroidRuntime(634): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-06 11:05:47.651: E/AndroidRuntime(634): ... 11 more
12-06 11:10:47.981: I/Process(634): Sending signal. PID: 634 SIG: 9
答案 0 :(得分:1)
您将在Administrator.java的第37行获取NPE,即:
list.setAdapter(adapter);
你在布局R.layout.administrator
还要检查 usersList = h.getAllUsers(); out。
Log.d("size = ", usersList.size);