简体   繁体   中英

android.content.res.Resources$NotFoundException: String resource ID #0x42

I have a listview. When I click on each item I wanted to display an alert dialog with an edit text, then save the input value into an integer variable.But, it shows error. When I don't convert the input text then it works fine but when I use Integer.parseInt() then the app crashes. here's my code and logcat.

//CODE

/**
 * A simple {@link Fragment} subclass.
 */
public class All extends Fragment  {

    //EXPERIMENTAL CODE STARTS
    private VolleySingleton volleySingleton;
    private RequestQueue requestQueue;
    private RecyclerView allItemList;
    private ArrayList<Items> listItems=new ArrayList<>();
    private AdapterItemAll adapterItemAll;
    public static int ticketNum=5;
    DatabaseHelper myDb;
    public int quantity=0;
    public float totalPrice=0;
    private String title;
    private boolean update;
    private String name;

    String strQuantity;

    EditText quantityInput;


    //SETTING IMAGE TEST CODE STARTS





    //public static final String URL_API="http://www.delaroystudios.com/images/teams.json";

                    //ENDS
         public All() {
        // Required empty public constructor
                 }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_all, container, false);

//        allItemList=(RecyclerView) view.findViewById(R.id.allItemsList);
//        allItemList.setLayoutManager(new LinearLayoutManager(getActivity()));
//        adapterItemAll=new AdapterItemAll(getActivity());
//        allItemList.setAdapter(adapterItemAll);




        //****LISTVIEW EXPERIMENT STARTS
            ListView listView=(ListView) view.findViewById(R.id.allItemsList);
            myDb=new DatabaseHelper(getContext());

            ArrayList<String> theList=new ArrayList<>();
        final Cursor data=myDb.getItemContents();

        if(data.getCount()==0){
            Toast.makeText(getActivity(),"List is Empty",Toast.LENGTH_SHORT).show();
        }else{
            while(data.moveToNext()){
                theList.add(0,data.getString(1));

                ListAdapter listAdapter=new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,theList);
                listView.setAdapter(listAdapter );
            }
        }




        //LIST ITEM ONCLICK ADD QUANTITY TO TICKET STARTS
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                name = (String) parent.getItemAtPosition(position);


                //ALERT DECLARE

                AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
                builder.setTitle("Item Quantity");
                builder.setIcon(R.mipmap.ic_receipt_black_24dp);
                builder.setMessage("Enter Item Quantity");

                quantityInput=new EditText(getContext());
                builder.setView(quantityInput);
                //SET POSITIVE

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        strQuantity = quantityInput.getText().toString();
                        quantity=Integer.valueOf(strQuantity);

                        Toast.makeText(getActivity(),quantity,Toast.LENGTH_LONG).show();

                        Toast.makeText(getActivity(),"Enter the correct quantity",Toast.LENGTH_LONG).show();

                    }
                });


                AlertDialog ad=builder.create();
                ad.show();
                //ENDS


                //updating quantity
                update=myDb.updateTicketQuantity(name,quantity);


                Toast.makeText(getActivity(),"Total "+quantity+ "Items Added to the Ticket",Toast.LENGTH_LONG).show();

            }
        });
        //ENDS

        //LISTVIEW EXPERIMENT ENDS




        return view;
    }


    public int quantityIncrease(int quantity){

        this.quantity=quantity+1;
        int qt=this.quantity;

        return qt;

    }

}

//Error log

E/AndroidRuntime: FATAL EXCEPTION: main
                  android.content.res.Resources$NotFoundException: String resource ID #0x42
                      at android.content.res.Resources.getText(Resources.java:230)
                      at android.widget.Toast.makeText(Toast.java:265)
                      at com.soumya.possystem.All$1$1.onClick(All.java:142)
                      at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:137)
                      at android.app.ActivityThread.main(ActivityThread.java:5041)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:511)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                      at dalvik.system.NativeStart.main(Native Method)

try this:

 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {


                    strQuantity = quantityInput.getText().toString();

                    quantity= Integer.parseInt(strQuantity);  //int

                    Toast.makeText(getActivity(),String.valueOf(quantity),Toast.LENGTH_LONG).show(); //error here

                    Toast.makeText(getActivity(),"Enter the correct quantity",Toast.LENGTH_LONG).show();

                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM