Android Studio NumberFormatException导致崩溃

时间:2019-01-08 22:08:30

标签: java android database android-studio

每当我尝试运行我的活动时, 我总是在电话线上撞车

total+=(Integer.parseInt(order.getPrice()))*(Integer.parseInt(order.getQuantity()));

实际上所有的loadListFood方法都崩溃了!

我的活动

public class Cart extends AppCompatActivity {
   RecyclerView recyclerView;
   RecyclerView.LayoutManager layoutManager;
   //
   FirebaseDatabase database;
   DatabaseReference requests;
   //
   TextView txtTotalPrice;
   Button btnPlace;

List<Order>  cart = new ArrayList<>();


CartAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        //// Firebase
        database = FirebaseDatabase.getInstance();
        requests = database.getReference("Requests");
        // INIT
        recyclerView =findViewById(R.id.listCart);
        recyclerView.setHasFixedSize(true);
        layoutManager  = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //
        txtTotalPrice =findViewById(R.id.total);
        btnPlace =findViewById(R.id.btnPlaceOrder);
      btnPlace.setOnClickListener(new View.OnClickListener() {
       @Override
        public void onClick(View v) {
           /// Create new Request
           showAlertDialoge();
        }
    });
        loadListFood();
    }



    private void loadListFood() {
        cart = new Database(this).getCarts();
        adapter = new CartAdapter(cart,this);
        recyclerView.setAdapter(adapter);
        //calculation

        int total =0;
        for(Order order :cart)
            total+=(Integer.parseInt(order.getPrice()))*(Integer.parseInt(order.getQuantity()));
        Locale locale= new Locale("en","US");
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        txtTotalPrice.setText(fmt.format(total));

        }



    }

logcat:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.OverRout.app/com.OverRout.app.Cart}: java.lang.NumberFormatException: For input string: "Hunger "
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.NumberFormatException: For input string: "Okay"
        at java.lang.Integer.parseInt(Integer.java:521)
        at java.lang.Integer.parseInt(Integer.java:556)
        at com.OverRout.app.Cart.loadListFood(Cart.java:128)
        at com.OverRout.app.Cart.onCreate(Cart.java:66)
        at android.app.Activity.performCreate(Activity.java:6679)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

1 个答案:

答案 0 :(得分:0)

这行在这里说出了什么问题:

  

由以下原因引起:java.lang.NumberFormatException:对于输入字符串:“好”

您正在尝试将字符串“ Okay”转换为数字,但是这将永远无法工作,您需要某种类型的数字,例如“ 65”。

如果这是输入,则应考虑将输入设置为数字,这样键盘将没有任何字母。