我面临一个愚蠢的问题。我只是想将数据添加到我的类的对象中,而不是成功。
我通过片段中的异步任务获取数据,并希望使用生成的JSON来填充我的ListView。我正在使用ArrayAdapter并为此创建了一个类。
我已经解析了JSON字符串,现在我正在尝试将从API检索到的数据添加到我的类的对象中,但它给了我'java.lang.NullPointerException'。
我想我无法将数据添加到班级的对象中。但我不明白其中的原因,我更习惯于在C#中使用,但JAVA是Object_Oriented,它应该是一样的,对吧?使用的代码如下......
JSONArray jsonArrayPendingBills = new JSONArray();
jsonArrayPendingBills = jsonObjectPendingBills.getJSONArray(TAG_RESPONSE_MESSAGE);
ArrayList<HashMap<String, String>> arrayPendingBills = new ArrayList<HashMap<String,String>>();
ArrayList<PendingBills> arrayClassPendingBills = new ArrayList<PendingBills>();
for(int i = 0;i < jsonArrayPendingBills.length(); i++){
JSONObject jsonObjectPendingBill = jsonArrayPendingBills.getJSONObject(i);
String strDate = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_DATE);
String strTxCode = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_TX_CODE);
String strFromAccount = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_FROM_ACCOUNT);
String strAmount = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_AMOUNT);
String strDetails = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_DETAILS);
String strBillCode = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_BILL_CODE);
PendingBills objPendingBills = new PendingBills();
objPendingBills.setBillAmount(strAmount);
objPendingBills.setBillDate(strDate);
objPendingBills.setBillName(strFromAccount);
//arrayClassPendingBills.add(new PendingBills(strAmount, strDate, strFromAccount));
arrayClassPendingBills.add(objPendingBills);
}
PendingBillsAdapter pendingBillsAdapter = new PendingBillsAdapter(getActivity(), R.layout.pending_bills_list_view_row, arrayClassPendingBills);
listViewPendingBills.setAdapter(pendingBillsAdapter);
progressDialogPendingBills.dismiss();
}
catch (Exception e){
e.printStackTrace();
Log.i("Error", "Error", e);
}
这是我班级的代码:
public class PendingBills {
// Declaring the strings for the class
public String BillAmount;
public String BillDate;
public String BillName;
// Writing down the constructor for this class
public PendingBills(String BillAmount, String BillDate, String BillName){
this.BillAmount = BillAmount;
this.BillDate = BillDate;
this.BillName = BillName;
}
public PendingBills(){
}
// Getter for BillAmount
public String getBillAmount(){
return BillAmount;
}
// Setter for BillAmount
public void setBillAmount(String BillAmount){
this.BillAmount = BillAmount;
}
public String getBillDate(){
return BillDate;
}
public void setBillDate(String BillDate){
this.BillDate = BillDate;
}
public String getBillName(){
return BillName;
}
public void setBillName(String BillName){
this.BillName = BillName;
}
}
我想我错过了一些东西。我不确定是什么?我在StackOverflow上查了几个与此相关的问题。我找到了很多,但解决方案对我不起作用。
How to properly add data to ArrayList - Android
任何帮助将不胜感激。谢谢。
这是LogCat ....
12-02 07:02:04.235: E/AndroidRuntime(4648): FATAL EXCEPTION: main
12-02 07:02:04.235: E/AndroidRuntime(4648): java.lang.NullPointerException
12-02 07:02:04.235: E/AndroidRuntime(4648): at com.zipcash.zipcashbetaversion.PendingBillsAdapter.getView(PendingBillsAdapter.java:43)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.ListView.onMeasure(ListView.java:1158)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1029)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-02 07:02:04.235: E/AndroidRuntime(4648): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.View.measure(View.java:15518)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.Choreographer.doFrame(Choreographer.java:532)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.os.Handler.handleCallback(Handler.java:725)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.os.Looper.loop(Looper.java:137)
12-02 07:02:04.235: E/AndroidRuntime(4648): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-02 07:02:04.235: E/AndroidRuntime(4648): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 07:02:04.235: E/AndroidRuntime(4648): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 07:02:04.235: E/AndroidRuntime(4648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-02 07:02:04.235: E/AndroidRuntime(4648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-02 07:02:04.235: E/AndroidRuntime(4648): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
试试这个..
像以下代码一样夸大您的布局。 context.getSystemService
或this
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
或
LayoutInflater inflater = LayoutInflater.from(context);
答案 1 :(得分:0)
k dude只是将arrayPendingBills维护为静态或使用像单例类一样的datacontroller类,只需将arrayPendingBills保持为静态然后运行就可以了:)祝你好运
答案 2 :(得分:0)
- add try & Catch to handle nullPointerException & any Exceptions
- Make trace the code to find where made excption.
- add system.out.Println to print the values then they find the exception
尝试{
JSONArray jsonArrayPendingBills = new JSONArray(); jsonArrayPendingBills = jsonObjectPendingBills.getJSONArray(TAG_RESPONSE_MESSAGE); 的ArrayList&GT; arrayPendingBills = new ArrayList&gt;(); ArrayList arrayClassPendingBills = new ArrayList();for(int i = 0;i < jsonArrayPendingBills.length(); i++){ JSONObject jsonObjectPendingBill = jsonArrayPendingBills.getJSONObject(i); String strDate = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_DATE); String strTxCode = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_TX_CODE); String strFromAccount = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_FROM_ACCOUNT); String strAmount = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_AMOUNT); String strDetails = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_DETAILS); String strBillCode = jsonObjectPendingBill.getString(TAG_PENDING_BILLS_BILL_CODE); PendingBills objPendingBills = new PendingBills(); objPendingBills.setBillAmount(strAmount); objPendingBills.setBillDate(strDate); objPendingBills.setBillName(strFromAccount); //arrayClassPendingBills.add(new PendingBills(strAmount, strDate, strFromAccount)); arrayClassPendingBills.add(objPendingBills); } PendingBillsAdapter pendingBillsAdapter = new PendingBillsAdapter(getActivity(), R.layout.pending_bills_list_view_row, arrayClassPendingBills); listViewPendingBills.setAdapter(pendingBillsAdapter); progressDialogPendingBills.dismiss(); }catch ( NullPointerException e) { e.printStackTrace(); Log.i("Error", "Error", e); } catch (Exception e){ e.printStackTrace(); Log.i("Error", "Error", e); }