【问题标题】:android.content.res.Resources$NotFoundException: String resource ID #0x42android.content.res.Resources$NotFoundException:字符串资源 ID #0x42
【发布时间】:2017-02-12 10:14:54
【问题描述】:

我有一个列表视图。当我单击每个项目时,我想显示一个带有编辑文本的警报对话框,然后将输入值保存到一个整数变量中。但是,它显示错误。当我不转换输入文本时,它工作正常,但是当我使用 Integer.parseInt() 时,应用程序崩溃。这是我的代码和 logcat。

//代码

/**
 * 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;

    }

}

//错误日志

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)

【问题讨论】:

  • Toast.makeText(getActivity(),String.valueOf(quantity),Toast.LENGTH_LONG).show();
  • 好的。但我需要将值转换为整数并将其保存到我的数量变量中。在吐司上展示它们不是我的目标。
  • 哎呀!!我的坏兄弟。我错过了你的观点。现在工作得很好。谢谢。

标签: java android android-studio android-alertdialog


【解决方案1】:

试试这个:

 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();

                }
            });

【讨论】:

  • 我认为不需要使用额外的String变量,因为需要在Toast中显示,所以在传递给makeText方法时只需使用String.valueOf(quantity)
  • thnkx @ρяσѕρєяK 提出建议
猜你喜欢
  • 2013-06-27
  • 2021-05-09
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 2023-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多