我有一个列表视图,当点击它时会打开一个对话框,其中列表项被编辑,并且在提交更改时会返回到列表视图。我的问题是,当我尝试重新加载listview时,我得到一个nullpointerexception。
函数getCurrentSalesRecords()用于在第一次调用活动时和更新后填充listview:
public ArrayList<SalesReciepts> getCurrentSalesRecords() {
ArrayList<SalesReciepts> sale = new ArrayList<SalesReciepts>();
Double invoice_id = Double.parseDouble(GlobalApp.data().id);
SQLiteDatabase db = this.getReadableDatabase();
String sql = "";
sql += "SELECT entry_id, product_description, quantity, total FROM " + SalesReceiptsConstants.NewConstants.ENTRY_TABLE_NAME;
sql += " WHERE " + SalesReceiptsConstants.NewConstants.INVOICE_NO + " = " + invoice_id;
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
SalesReciepts sales = new SalesReciepts();
sales.setEntryId(cursor.getInt(0));
sales.setProduct(cursor.getString(1));
sales.setQty(cursor.getInt(2));
sales.setTotal(cursor.getDouble(3));
sale.add(sales);
}
return sale;
}
选项卡主机中的onFinishDialog()方法:
public void onFinishDialog() {
if (confirm != null) {
confirm.updateView();
//mTabHost.setCurrentTab(2);
}
}
列表视图片段:
public class confirmFragmentTab extends Fragment implements OnItemClickListener,
OnItemLongClickListener {
public static final String ARG_ITEM_ID = "sales_list";
//Context context;
ListView saleListView;
ArrayList<SalesReciepts> salesReciept;
SalesListAdapter salesListAdapter;
ProductsDbHelper db;
Activity activity;
private GetEmpTask task;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
db = new ProductsDbHelper(getActivity());
//saleListView = new ListView(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.confirm_layout, container, false);
findViewsById(rootView);
task = new GetEmpTask(getActivity());
task.execute((Void) null);
saleListView.setOnItemClickListener(this);
saleListView.setOnItemLongClickListener(this);
return rootView;
}
private void findViewsById(View view) {
saleListView = (ListView) view.findViewById(R.id.list_sale);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onItemClick(AdapterView<?> list, View arg1, int position,
long arg3) {
SalesReciepts sale = (SalesReciepts) list.getItemAtPosition(position);
if (sale != null) {
Bundle arguments = new Bundle();
arguments.putParcelable("selectedSale", sale);
CustomEditDialog customDialogFragment = new CustomEditDialog();
customDialogFragment.setArguments(arguments);
customDialogFragment.show(getFragmentManager(),
CustomEditDialog.ARG_ITEM_ID);
}
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long arg3) {
SalesReciepts sale = (SalesReciepts) parent.getItemAtPosition(position);
// Use AsyncTask to delete from database
db.delete(sale);
salesListAdapter.remove(sale);
return true;
}
public class GetEmpTask extends AsyncTask<Void, Void, ArrayList<SalesReciepts>> {
private final WeakReference<Activity> activityWeakRef;
public GetEmpTask(Activity context)
{
this.activityWeakRef = new WeakReference<Activity>(context);
}
@Override
protected ArrayList<SalesReciepts> doInBackground(Void... arg0) {
ArrayList<SalesReciepts> saleList = db.getCurrentSalesRecords();
return saleList;
}
@Override
protected void onPostExecute(ArrayList<SalesReciepts> sList) {
if (activityWeakRef.get() != null
&& !activityWeakRef.get().isFinishing()) {
Log.d("sales", sList.toString());
salesReciept = sList;
if (sList != null) {
if (sList.size() != 0) {
salesListAdapter = new SalesListAdapter(activity,
sList);
saleListView.setAdapter(salesListAdapter);
} else {
Toast.makeText(getActivity(), "No Sales Records",
Toast.LENGTH_LONG).show();
}
}
}
}
}
/*
* This method is invoked from Invoice3 onFinishDialog() method. It is
* called from CustomEditDialog when a sale record is updated.
* This is used for communicating between fragments.
*/
public void updateView() {
task = new GetEmpTask(activity);
task.execute((Void) null);
}
}
对话框片段:
public class CustomEditDialog extends DialogFragment {
// UI references
private EditText saleProduct;
private EditText saleQty;
private EditText saleTotal;
private LinearLayout submitLayout;
private SalesReciepts sales;
private static final SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd", Locale.ENGLISH);
ProductsDbHelper db;
public static final String ARG_ITEM_ID = "sale_dialog_fragment";
public interface saleDialogFragmentListener {
void onFinishDialog();
}
public CustomEditDialog() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
db = new ProductsDbHelper(getActivity());
Bundle bundle = this.getArguments();
sales = bundle.getParcelable("selectedSale");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View customDialogView = inflater.inflate(R.layout.fragment_add_sale,
null);
builder.setView(customDialogView);
saleProduct = (EditText) customDialogView.findViewById(R.id.editProduct);
saleQty = (EditText) customDialogView
.findViewById(R.id.editQty);
saleTotal = (EditText) customDialogView.findViewById(R.id.editTotal);
submitLayout = (LinearLayout) customDialogView
.findViewById(R.id.layout_submit);
submitLayout.setVisibility(View.GONE);
setValue();
builder.setTitle(R.string.update_sale);
builder.setCancelable(false);
builder.setPositiveButton(R.string.update,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
sales.setProduct(saleProduct.getText().toString());
sales.setQty(Integer.parseInt(saleQty.getText().toString()));
sales.setTotal(Double.parseDouble(saleTotal.getText().toString()));
long result = db.update(sales);
if (result > 0) {
Invoice3 invoice = (Invoice3)getActivity();
invoice.onFinishDialog();
} else {
Toast.makeText(getActivity(),
"Unable to update record",
Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
return alertDialog;
}
private void setValue() {
if (sales != null) {
saleProduct.setText(sales.getProduct());
saleQty.setText(sales.getQty() + "");
saleTotal.setText(String.valueOf(sales.getTotal()));
}
}
}
日志:
08-11 09:06:56.796 1700-1725/com.example.Prototype E/AndroidRuntime﹕
FATAL EXCEPTION: AsyncTask #2
Process: com.example.Prototype, PID: 1700
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.Prototype.confirmFragmentTab$GetEmpTask.doInBackground(confirmFragmentTab.java:115)
at com.example.Prototype.confirmFragmentTab$GetEmpTask.doInBackground(confirmFragmentTab.java:104)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
空指针在:
protected ArrayList<SalesReciepts> doInBackground(Void... arg0) {
ArrayList<SalesReciepts> saleList = db.getCurrentSalesRecords();
return saleList;
}
更新
放置
db = new ProductsDbHelper(getActivity());
在doInBackground()中。仍然得到空指针错误但无法识别源。
LOG:
08-11 09:45:18.347 1253-1495/system_process W/InputMethodManagerService﹕ Got RemoteException sending setActive(false) notification to pid 1711 uid 10052
08-11 09:45:18.417 1416-1445/com.android.inputmethod.latin W/Binder﹕ Caught a RuntimeException from the binder stub implementation.
java.lang.NullPointerException
at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
08-11 09:45:19.747 1253-1267/system_process I/ActivityManager﹕ Displayed com.example.Prototype/.Check: +2s441ms
更新
改变 db = new ProductsDbHelper(thisActivity()); 至 db = new ProductsDbHelper(activity);
空指针异常:
db = getReadableDatabase();
LOG:
08-11 10:15:55.901 1739-1784/com.example.Prototype E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.Prototype, PID: 1739
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at com.example.Prototype.ProductsDbHelper.getCurrentSalesRecords(ProductsDbHelper.java:352)
at com.example.Prototype.confirmFragmentTab$GetEmpTask.doInBackground(confirmFragmentTab.java:109)
at com.example.Prototype.confirmFragmentTab$GetEmpTask.doInBackground(confirmFragmentTab.java:97)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
答案 0 :(得分:0)
尝试以下方法:
td
删除onCreate