我正在尝试从数据库中获取名称并在List中显示结果。但我得到Null Pointer Exception。我使用自定义的ArrayAdapter在List中显示名称。
FragmentGroups.java: -
package com.sunmobile.navigationdrawer;
import java.util.ArrayList;
import java.util.List;
import android.app.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.sunmobileappnow.mobileappnow.R;
public class FragmentGroups extends Fragment {
SendJobDataBase db;
Cursor cursor;
ArrayList<member> membr;
ListView list1 ;
ListView list2 ;
Button group1;
Button group2;
GroupAdapter groupadapter;
public FragmentGroups(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.group_fragment, container, false);
group1 = (Button) rootView.findViewById(R.id.Gp1);
group2 = (Button) rootView.findViewById(R.id.Gp2);
list1 = (ListView) rootView.findViewById(R.id.list1);
membr=getMembersList();
groupadapter= new GroupAdapter(this.getActivity(),R.layout.listrow_item ,membr);
list1.setAdapter((ListAdapter) groupadapter);
String[] values2 = new String[] { "Cricket",
"Hockey",
"footBall",
"BasketBall",
"Soccer",
"Badminton",
"Tennis",
};
list2 = (ListView) rootView.findViewById(R.id.list2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String> (this.getActivity(),R.layout.listrow, values2);
list2.setAdapter(adapter2);
group1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
list1.setVisibility(View.VISIBLE);
list2.setVisibility(View.INVISIBLE);
}
});
group2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
list1.setVisibility(View.GONE);
list2.setVisibility(View.VISIBLE);
}
});
return rootView;
}
public ArrayList<member> getMembersList() {
ArrayList<member> user = new ArrayList<member>();
String selectQuery = "SELECT sename FROM jobsend" ;
this.db = new SendJobDataBase(getActivity());
this.db.getWritableDatabase();
cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
member contact = new member();
contact.setName(cursor.getString(1));
user.add(contact);
} while (cursor.moveToNext());
}
return user;
}
}
SendJobDataBase.java: -
package com.sunmobile.navigationdrawer;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import java.util.ArrayList;
import java.util.List;
public class SendJobDataBase
{
private static final String DATABASE_NAME = "myjob.db";
private static final int DATABASE_VERSION = 2;
static final String TABLE_NAME = "jobsend";
private static Context context;
static SQLiteDatabase db;
Cursor cursor;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME + " (sename,smobno,spicktime,spickaddr,sfair,ssendjob) values (?,?,?,?,?,?)";
protected static final String enternpw = null;
public SendJobDataBase(Context context) {
SendJobDataBase.context = context;
OpenHelper openHelper = new OpenHelper(SendJobDataBase.context);
SendJobDataBase.db = openHelper.getWritableDatabase();
this.insertStmt = SendJobDataBase.db.compileStatement(INSERT);
}
public long insert(String sename,String smobno,String spicktime,String spickaddr,String sfair,String ssendjob) {
this.insertStmt.bindString(1, sename);
this.insertStmt.bindString(2, smobno);
this.insertStmt.bindString(3, spicktime);
this.insertStmt.bindString(4, spickaddr);
this.insertStmt.bindString(5, sfair);
this.insertStmt.bindString(6, ssendjob);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
db.delete(TABLE_NAME, null, null);
}
public List<String[]> selectAll()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(TABLE_NAME, new String[] { "id","sename","smobno","spicktime","spickaddr","sfair","ssendjob" }, null, null, null, null, "sename asc");
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),
cursor.getString(3),cursor.getString(4),cursor.getString(5),
cursor.getString(6)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
public void delete(int rowId) {
db.delete(TABLE_NAME, null, null);
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, sename TEXT, smobno TEXT,spicktime TEXT,spickaddr TEXT, sfair TEXT,ssendjob TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public Cursor rawQuery(String selectQuery, Object object) {
// TODO Auto-generated method stub
return cursor;
}
public static SendJobDataBase getDBAdapterInstance(FragmentGroups viewPassword) {
// TODO Auto-generated method stub
return null;
}
public Object getenternpw() {
// TODO Auto-generated method stub
return null;
}
public static Cursor getSinlgeEntry() {
// TODO Auto-generated method stub
Cursor cur =db.rawQuery("SELECT * FROM "+ TABLE_NAME, null);
// colName1+"='"+name+"'" -> wherclause
System.out.println("Record count are "+cur.getCount());
return cur;
}
public static void close() {
// TODO Auto-generated method stub
}
public SendJobDataBase open() {
// TODO Auto-generated method stub
return null;
}
public SendJobDataBase getWritableDatabase() {
// TODO Auto-generated method stub
return null;
}
}
错误: -
03-31 01:54:00.891: E/AndroidRuntime(1086): FATAL EXCEPTION: main
03-31 01:54:00.891: E/AndroidRuntime(1086): Process: com.sunmobileappnow.mobileappnow, PID: 1086
03-31 01:54:00.891: E/AndroidRuntime(1086): java.lang.NullPointerException
03-31 01:54:00.891: E/AndroidRuntime(1086): at com.sunmobile.navigationdrawer.FragmentGroups.getMembersList(FragmentGroups.java:105)
03-31 01:54:00.891: E/AndroidRuntime(1086): at com.sunmobile.navigationdrawer.FragmentGroups.onCreateView(FragmentGroups.java:48)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.app.Fragment.performCreateView(Fragment.java:1700)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.app.BackStackRecord.run(BackStackRecord.java:684)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.os.Handler.handleCallback(Handler.java:733)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.os.Handler.dispatchMessage(Handler.java:95)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.os.Looper.loop(Looper.java:137)
03-31 01:54:00.891: E/AndroidRuntime(1086): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-31 01:54:00.891: E/AndroidRuntime(1086): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 01:54:00.891: E/AndroidRuntime(1086): at java.lang.reflect.Method.invoke(Method.java:515)
03-31 01:54:00.891: E/AndroidRuntime(1086): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-31 01:54:00.891: E/AndroidRuntime(1086): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-31 01:54:00.891: E/AndroidRuntime(1086): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
对于来自logcat的错误,
在getMembersList()
,cursor
让你成为NPE,你可以这样验证:
之前
if (cursor.moveToFirst()) {
检查:
if(cursor != null && cursor.getCount() > 0)
此外,您可以/需要为全局变量membr
的结果操作设置为null,该数据作为数据传递给groupadapter