当您点击图标时,应用程序崩溃。

huangapple go评论92阅读模式
英文:

When you click on the icon, the application crashes

问题

当您点击图标时,应用程序崩溃。
如果我在首次从主活动启动应用程序时进入垃圾桶。一切都没问题。然后我将物品添加到购物车。一切也都好。但是后来返回主页,尝试进入购物篮,我遇到应用程序崩溃和以下错误

我的CARTACTIVITY

package com.example.tucanofood;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tucanofood.Common.Common;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.rey.material.widget.Button;

import org.w3c.dom.Text;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import DataBase.Database;
import Model.Order;
import Model.Request;
import ViewHolder.CartAdapter;

public class Cart extends AppCompatActivity {
    // ... (此处省略了一部分代码)
    // ... (下面是您提供的代码中的一部分,已经过翻译)
    
    private void loadlistFood() {
        carts = new Database(this).getCarts();
        adapter = new CartAdapter(carts,this);
        recyclerView.setAdapter(adapter);

        int total = 0;

        for (Order order:carts)
            total += (Integer.parseInt(order.getPrice())) * (Integer.parseInt(order.getQuantity()));

        Locale locale = new Locale("de", "GER");
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);

        txtTotalPrice.setText(fmt.format(total));
    }
}

我的LOGCAT错误 138 和 72

--------- 崩溃开始
2020-08-21 11:40:51.591 4973-4973/com.example.tucanofood E/AndroidRuntime: FATAL EXCEPTION: 
main
Process: com.example.tucanofood, PID: 4973
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.tucanofood/com.example.tucanofood.Cart}: 
java.lang.NumberFormatException: For input string: "Tucano Espresso Decaf"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NumberFormatException: For input string: "Tucano Espresso Decaf"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.tucanofood.Cart.loadlistFood(Cart.java:138)
at com.example.tucanofood.Cart.onCreate(Cart.java:72)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
英文:

When you click on the icon, the application crashes.
If I enter when I first launch the app from home activity to the trash. All OK. Then I add the item to the cart. Also everything is ok. But then returning to home and trying to enter the basket, I get an application crash and the following error

MY CARTACTIVITY

package com.example.tucanofood;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.tucanofood.Common.Common;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.rey.material.widget.Button;
import org.w3c.dom.Text;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import DataBase.Database;
import Model.Order;
import Model.Request;
import ViewHolder.CartAdapter;
public class Cart extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference requests;
TextView txtTotalPrice;
Button btnPlace;
List<Order> carts = new ArrayList<>();
CartAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
database =  FirebaseDatabase.getInstance();
requests = database.getReference("Requests");
recyclerView = (RecyclerView) findViewById(R.id.listCart);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
txtTotalPrice = (TextView) findViewById(R.id.total);
btnPlace = (Button) findViewById(R.id.BtnPlaceOrder);
btnPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAlertDialog();
}
});
loadlistFood();
}
private void showAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
alertDialog.setTitle("Noch ein Schritt!");
alertDialog.setMessage("Eingeben deine Adresse");
final EditText editAdresse = new EditText(Cart.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
editAdresse.setLayoutParams(lp);
alertDialog.setView(editAdresse);
alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);
alertDialog.setPositiveButton("JA", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Request request = new Request(
Common.currentUser.getPhone(),
Common.currentUser.getVorname(),
editAdresse.getText().toString(),
txtTotalPrice.getText().toString(),
carts
);
requests.child(String.valueOf(System.currentTimeMillis()))
.setValue(request);
new Database(getBaseContext()).cleanCart();
Toast.makeText(Cart.this, "Vielen Dank, dass Sie Platz bestellen", Toast.LENGTH_SHORT).show();
finish();
}
});
alertDialog.setNegativeButton("NEIN", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
private void loadlistFood() {
carts = new Database(this).getCarts();
adapter = new CartAdapter(carts,this);
recyclerView.setAdapter(adapter);
int total = 0;
for (Order order:carts)
total += (Integer.parseInt(order.getPrice())) * (Integer.parseInt(order.getQuantity()));
Locale locale = new Locale("de", "GER");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
txtTotalPrice.setText(fmt.format(total));
}
}

MY LOGCAT Error 138 und 72

 --------- beginning of crash
2020-08-21 11:40:51.591 4973-4973/com.example.tucanofood E/AndroidRuntime: FATAL EXCEPTION: 
main
Process: com.example.tucanofood, PID: 4973
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.tucanofood/com.example.tucanofood.Cart}: 
java.lang.NumberFormatException: For input string: "Tucano Espresso Decaf"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NumberFormatException: For input string: "Tucano Espresso Decaf"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.tucanofood.Cart.loadlistFood(Cart.java:138)
at com.example.tucanofood.Cart.onCreate(Cart.java:72)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)

答案1

得分: 1

你的问题出在这一行:
total+=(Integer.parseInt(order.getPrice()))*(Integer.parseInt(order.getQuantity()));

order.getQuantity() 或者 order.getPrice() 其中之一没有返回预期的结果。你得到了 "Tucano Espresso Decaf" 而不是数量或价格。
请检查这两个方法,或者将它们贴在这里,这样我们可以查看一下。

英文:

Your problem is at this line:
total+=(Integer.parseInt(order.getPrice()))*(Integer.parseInt(order.getQuantity()));

One of order.getQuantity() or order.getPrice() are not returning the expected result. You get "Tucano Espresso Decaf" returned instead of quantity or price.
Review these two methods or post them here so we can take a look.

答案2

得分: 0

看起来是 AlertDialog 实现的问题。
尝试一下这个方法:

enter image description here

英文:

Looks like a problem with AlertDialog implementation.
Try this once,

enter image description here

huangapple
  • 本文由 发表于 2020年8月21日 16:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63519582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定