我最近尝试将一个正在运行的sqlite填充列表视图活动更改为一个片段。我已经做了所需的更改,但我继续得到我不明白的错误。如果你们中的任何人能够提供帮助,那将会减轻严重的挫败感。
这是我的主要活动
public class FoursFragment extends Fragment {
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private ArrayList<String> userId = new ArrayList<String>();
private ArrayList<String> user_fName = new ArrayList<String>();
private ArrayList<String> user_lName = new ArrayList<String>();
private ListView userList;
private AlertDialog.Builder build;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.display_activity, container, false);
userList = (ListView) view.findViewById(R.id.List);
mHelper = new DbHelper(getActivity());
//add new record
getView().findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getActivity(),
AddActivity.class);
i.putExtra("update", false);
startActivity(i);
}
});
//click to update data
userList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getActivity(),
AddActivity.class);
i.putExtra("Fname", user_fName.get(arg2));
i.putExtra("Lname", user_lName.get(arg2));
i.putExtra("ID", userId.get(arg2));
i.putExtra("update", true);
startActivity(i);
}
});
//long click to delete data
userList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
build = new AlertDialog.Builder(getActivity());
build.setTitle("Delete " + user_fName.get(arg2) + " "
+ user_lName.get(arg2));
build.setMessage("Do you want to delete ?");
build.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(
getActivity(),
user_fName.get(arg2) + " "
+ user_lName.get(arg2)
+ " is deleted.", 3000).show();
dataBase.delete(
DbHelper.TABLE_NAME,
DbHelper.KEY_ID + "="
+ userId.get(arg2), null);
displayData();
dialog.cancel();
}
});
build.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();
return true;
}
});
return view;
}
@Override
public void onResume() {
displayData();
super.onResume();
}
/**
* displays data from SQLite
*/
private void displayData() {
dataBase = mHelper.getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
+ DbHelper.TABLE_NAME, null);
userId.clear();
user_fName.clear();
user_lName.clear();
if (mCursor.moveToFirst()) {
do {
userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME)));
user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME)));
} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(getActivity(),userId, user_fName, user_lName);
userList.setAdapter(disadpt);
mCursor.close();
}
}
这是调用的适配器。
public class DisplayAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> firstName;
private ArrayList<String> lastName;
public DisplayAdapter(Context c, ArrayList<String> id,ArrayList<String> fname, ArrayList<String> lname) {
this.mContext = c;
this.id = id;
this.firstName = fname;
this.lastName = lname;
}
public int getCount() {
// TODO Auto-generated method stub
return id.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.listcell, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
mHolder.txt_fName = (TextView) child.findViewById(R.id.txt_fName);
mHolder.txt_lName = (TextView) child.findViewById(R.id.txt_lName);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(id.get(pos));
mHolder.txt_fName.setText(firstName.get(pos));
mHolder.txt_lName.setText(lastName.get(pos));
return child;
}
public class Holder {
TextView txt_id;
TextView txt_fName;
TextView txt_lName;
}
}
这是DBhelper。
public class DbHelper extends SQLiteOpenHelper {
static String DATABASE_NAME="userdata";
public static final String TABLE_NAME="user";
public static final String KEY_FNAME="fname";
public static final String KEY_LNAME="lname";
public static final String KEY_ID="id";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_FNAME+" TEXT, "+KEY_LNAME+" TEXT)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}
我收到的错误如下
03-20 13:15:50.108: E/AndroidRuntime(795): FATAL EXCEPTION: main
03-20 13:15:50.108: E/AndroidRuntime(795): java.lang.NullPointerException
03-20 13:15:50.108: E/AndroidRuntime(795): atapp.norman.tennis.FoursFragment.onCreateView(FoursFragment.java:52)
03-20 13:15:50.108: E/AndroidRuntime(795): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
03-20 13:15:50.108: E/AndroidRuntime(795): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
03-20 13:15:50.108: E/AndroidRuntime(795): at android.app.BackStackRecord.run(BackStackRecord.java:622)
03-20 13:15:50.108: E/AndroidRuntime(795): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
03-20 13:15:50.108: E/AndroidRuntime(795): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:417)
03-20 13:15:50.108: E/AndroidRuntime(795): at android.os.Handler.handleCallback(Handler.java:605)
03-20 13:15:50.108: E/AndroidRuntime(795): at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 13:15:50.108: E/AndroidRuntime(795): at android.os.Looper.loop(Looper.java:137)
03-20 13:15:50.108: E/AndroidRuntime(795): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-20 13:15:50.108: E/AndroidRuntime(795): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 13:15:50.108: E/AndroidRuntime(795): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 13:15:50.108: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-20 13:15:50.108: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-20 13:15:50.108: E/AndroidRuntime(795): at dalvik.system.NativeStart.main(Native Method)
我真的不介意这是不是很蠢,但真的无法弄清楚发生了什么!三江源!
答案 0 :(得分:2)
替换
//add new record
getView().findViewById(...
与
//add new record
view.findViewById(...
getView()
中的{p> onCreateView()
为时尚早 - 您尚未将视图返回到getView()
返回的框架。