尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在null对象引用中在json中

时间:2018-06-12 03:41:45

标签: android json android-recyclerview settext

我正在尝试从json文件中调用折扣价折扣价

Json Content是。

[  
   {  
      "Menu_ID":"63",

      "Menu_name":"Who knows that he is hero? whole of product discre",
      "Price":"1800",
      "Discounted_Price":"180",
      "Discounted_Value":"13",
      "Menu_image":"upload\/images\/9489-2018-06-11.png"
   },
   {  
      "Menu_ID":"62",
      "Menu_name":"Who knows that he is hero?",
      "Price":"190",
      "Discounted_Price":"90",
      "Discounted_Value":"9",
      "Menu_image":"upload\/images\/0459-2018-06-10.png"
   }
]

我正在使用recyclerview显示我应用中的所有内容。

片段我实施 recyclerview

的地方
ArrayList<Double> Discounted_Price = new ArrayList<Double>();
ArrayList<String> Discounted_Value = new ArrayList<String>();

public void PrsJsonRea(JSONArray array){

    for(int i = 0; i<array.length(); i++) {

        SecondPojoListData getJsonRecAdap = new SecondPojoListData();

        JSONObject json = null;
        try {

            json = array.getJSONObject(i);

            getJsonRecAdap.setDiscPrice3(json.getString("Discounted_Price"));
            getJsonRecAdap.setDiscValue3(json.getString("Discounted_Value"));

            Menu_ID3.add(json.getLong("Menu_ID"));

            // Adding image title name in array to display on RecyclerView click event.
            Discounted_Value5.add(json.getString("Discounted_Value"));
            Discounted_Price5.add(Double.valueOf(formatData.format(json.getDouble("Discounted_Price"))));

        } catch (JSONException e) {

            e.printStackTrace();
        }
        recDataAdap.add(getJsonRecAdap);
    }

    madap2 = new FrontHorizontalListAdapter(recDataAdap,getContext());
    secondrecyclerView.setAdapter(madap2);
}

适配器类

@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {

    SecondPojoListData dataAdapterOBJ =  dataAdapters.get(position);

    Viewholder.discountedvalue.setText(dataAdapterOBJ.getDiscValue3());
    Viewholder.discountedprice.setText(dataAdapterOBJ.getDiscPrice3());


}

类ViewHolder扩展了RecyclerView.ViewHolder {

    public TextView discountedvalue, discountedprice;
    public 

    public ViewHolder(View itemView) {

        super(itemView);
        discountedprice = (TextView) itemView.findViewById(R.id.DiscountPrice);
        discountedvalue = (TextView) itemView.findViewById(R.id.DiscountValue);

        Menuprice = (TextView) itemView.findViewById(R.id.itemprice);
    }
}

xml layout

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        android:textColor="@color/secondary_text"
        android:maxLines="1"
        android:gravity="left|center_vertical"
        android:id="@+id/DiscountValue"
        android:padding="5dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        android:textColor="@color/secondary_text"
        android:maxLines="1"
        android:gravity="left|center_vertical"
        android:id="@+id/DiscountPrice"
        android:padding="5dp"/>

错误讯息。

06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at com.androbaron.ecommerce.adapters.FrontHorizontalListAdapter.onBindViewHolder(FrontHorizontalListAdapter.java:83)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at com.androbaron.ecommerce.adapters.FrontHorizontalListAdapter.onBindViewHolder(FrontHorizontalListAdapter.java)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)

所有其他工作完全接受折扣价折扣价

有人帮我解决了这个错误吗?

4 个答案:

答案 0 :(得分:1)

您的错误是func configureCell() { label.textColor = turnOn ? .white : .black } ,因为NullPointerException中引用的TextView之一为空。在创建ViewHolder时,请查看您正在膨胀的布局,以了解您所引用的ID不在您绑定的ItemView中的原因。

答案 1 :(得分:1)

我通过更改布局值来解决问题。

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View viewTWO = LayoutInflater.from(parent.getContext()).inflate(R.layout.first_cardview, parent, false);
    ViewHolder rowTWO = new ViewHolder(viewTWO);
    return rowTWO;
}

感谢所有人。

答案 2 :(得分:1)

请输入类似于Double的字符串。

Viewholder.discountedvalue.setText(String.valueOf(dataAdapterOBJ.getDiscValue3()));

答案 3 :(得分:0)

您尝试绑定到Recyclerview的视图似乎配置错​​误。请仔细检查您使用的布局是否膨胀。如果您可以上传适配器类的全部内容,将会很有帮助。