简体   繁体   中英

Android app crashes with no logcat errors

I'm developing an android application for my Mobile App Development final project that is due tomorrow, and have run into a problem. Up until yesterday, my app would function correctly on my phone, but I somehow messed something up since then. The program runs perfectly, and does everything I need it to on the emulator, even after everything I've added to it since yesterday, so I assumed the app was completely finished and ready to present. However, I ran it on my phone today and found that the app is crashing upon launching the following activity...

public class OrderDetails extends AppCompatActivity {

    private List<PhoneBook> contactList;
    private ListView listView;
    private TextView name, number, address, orderTotal, amountReceived, tip, mileage, grandTotal;
    private Button add;
    private PhonebookAdapter adapter;
    PhoneBookHandler db;

    double dAmountReceived;
    double dTotalCost;
    double dSubtract;
    double dMileage;
    double dGrandTotal;
    double dTip;
    double dAdd;
    double dMilesDriven;
    double dMultiply;
    double dRatePerMile;
    String sAmountReceived;
    String sTotalCost;
    String sTip;
    String sMileage;
    String sGrandTotal;
    String sAddress;
    String ratePerDelivery;
    String ratePerMile;
    String milesDriven;
    EditText eAmountReceived;
    EditText eTotalCost;
    EditText eTip;
    EditText eMileage;
    EditText eAddress;
    EditText eGrandTotal;
    EditText eMilesDriven;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_details);

        listView = (ListView) findViewById(R.id.contactlist);

        db = new PhoneBookHandler(this);

        contactList = db.getAllContacts();

        adapter = new PhonebookAdapter(this, contactList);

        eAddress = (EditText) findViewById(R.id.address);

        eTotalCost = (EditText) findViewById(R.id.orderTotal);
        eAmountReceived = (EditText) findViewById(R.id.amountReceived);
        eTip = (EditText) findViewById(R.id.tip);
        eMileage = (EditText) findViewById(R.id.mileage);
        eGrandTotal = (EditText) findViewById(R.id.grandTotal);
        eMilesDriven = (EditText) findViewById(R.id.milesDriven);

        // use shared preferences to inflate mileage edittext with data entered in settings menu
        ratePerDelivery = PreferenceManager.getDefaultSharedPreferences(this).getString("ratePerDeliveryPref", null);
        ratePerMile = PreferenceManager.getDefaultSharedPreferences(this).getString("ratePerMilePref", null);

        eAmountReceived.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after){}

            @Override
            public void onTextChanged(CharSequence s, int start,
                                  int before, int count){}

            @Override
            public void afterTextChanged(Editable s)
            {
                if (getCurrentFocus() == eAmountReceived)
                {
                    sAmountReceived = eAmountReceived.getText().toString();
                    sTotalCost = eTotalCost.getText().toString();
                    sMileage = eMileage.getText().toString();
                    sTip = eTip.getText().toString();

                    try
                    {
                        dAmountReceived = Double.parseDouble(sAmountReceived);
                        dTotalCost = Double.parseDouble(sTotalCost);

                        dSubtract = dAmountReceived - dTotalCost;
                        dMileage = Double.parseDouble(sMileage);
                        dGrandTotal = dSubtract + dMileage;
                    } catch(NumberFormatException e){}

                    sTip = String.valueOf(dSubtract);

                    DecimalFormat df = new DecimalFormat("0.00");
                    sTip = df.format(dSubtract);

                    eTip.setText(sTip);

                    sGrandTotal = String.valueOf(dGrandTotal);
                    sGrandTotal = df.format(dGrandTotal);
                    eGrandTotal.setText(sGrandTotal);
                }
            }
        });


        eTip.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after){}

            @Override
            public void onTextChanged(CharSequence s, int start,
                                  int before, int count){}

            @Override
            public void afterTextChanged(Editable s) {
                if (getCurrentFocus() == eTip)
                {
                    sAmountReceived = eAmountReceived.getText().toString();
                    sTotalCost = eTotalCost.getText().toString();
                    sMileage = eMileage.getText().toString();
                    sTip = eTip.getText().toString();

                    try {
                        dTip = Double.parseDouble(sTip);
                        dTotalCost = Double.parseDouble(sTotalCost);

                        dAdd = dTotalCost + dTip;
                        dMileage = Double.parseDouble(sMileage);
                        dGrandTotal = dTip + dMileage;
                    } catch (NumberFormatException e) {
                    }

                    sAmountReceived = String.valueOf(dAdd);

                    DecimalFormat df = new DecimalFormat("0.00");
                    sAmountReceived = df.format(dAdd);

                    eAmountReceived.setText(sAmountReceived);

                    sGrandTotal = String.valueOf(dGrandTotal);
                    sGrandTotal = df.format(dGrandTotal);
                    eGrandTotal.setText(sGrandTotal);
                }
            }
        });


        if (ratePerMile.equals("") && !ratePerDelivery.equals(""))
        {
            eMileage.setText(ratePerDelivery);
        }

        else
        {
            eMilesDriven.addTextChangedListener(new TextWatcher()
            {
                @Override
                public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after){}

                @Override
                public void onTextChanged(CharSequence s, int start,
                                      int before, int count){}

                @Override
                public void afterTextChanged(Editable s)
                {
                    if (getCurrentFocus() == eMilesDriven)
                    {
                        sAmountReceived = eAmountReceived.getText().toString();
                        sTotalCost = eTotalCost.getText().toString();
                        sMileage = eMileage.getText().toString();
                        sTip = eTip.getText().toString();

                        try
                        {
                            milesDriven = eMilesDriven.getText().toString();
                            dMilesDriven = Double.parseDouble(milesDriven);
                            dRatePerMile = Double.parseDouble(ratePerMile);
                            dMultiply = dRatePerMile * dMilesDriven;
                            dGrandTotal = dSubtract + dMultiply;
                        } catch(NumberFormatException e){}

                        sMileage = String.valueOf(dMultiply);

                        DecimalFormat df = new DecimalFormat("0.00");
                        sMileage = df.format(dMultiply);

                        eMileage.setText(sMileage);

                        sGrandTotal = String.valueOf(dGrandTotal);
                        sGrandTotal = df.format(dGrandTotal);
                        eGrandTotal.setText(sGrandTotal);
                    }
                }
            });
        }
    }

    public void onClick(View view)
    {

        name = (EditText) findViewById(R.id.name);
        number = (EditText) findViewById(R.id.number);
        address = (EditText) findViewById(R.id.address);
        orderTotal = (EditText) findViewById(R.id.orderTotal);
        amountReceived = (EditText) findViewById(R.id.amountReceived);
        tip = (EditText) findViewById(R.id.tip);
        mileage = (EditText) findViewById(R.id.mileage);
        grandTotal = (EditText) findViewById(R.id.grandTotal);

        String cName = name.getText().toString();
        String num = number.getText().toString();
        String cAddress = address.getText().toString();
        String cOrderTotal = orderTotal.getText().toString();
        String cAmountReceived = amountReceived.getText().toString();
        String cTip = tip.getText().toString();
        String cMileage = mileage.getText().toString();
        String cGrandTotal = grandTotal.getText().toString();


        int id = db.addContact(new PhoneBook(cName, num, cAddress, cOrderTotal,
                                          cAmountReceived, cTip, cMileage, cGrandTotal));
        contactList.add(new PhoneBook(id, cName, num, cAddress, cOrderTotal,
                                   cAmountReceived, cTip, cMileage, cGrandTotal));
        adapter.notifyDataSetChanged();

        Toast.makeText(getApplicationContext(), "Entry Successfully Created.", Toast.LENGTH_LONG).show();
    }


    public void buttonClickFunction(View v)
    {
        Intent intent = new Intent(getApplicationContext(), display_database.class);
        startActivity(intent);
    }

    public void sendMessage(View v)
    {
        eAddress = (EditText) findViewById(R.id.address);
        sAddress = eAddress.getText().toString();

        String map = "http://maps.google.com/maps?q=" + sAddress;
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(map));
        startActivity(intent);
    }
}

The only thing I have added since yesterday is the TextWatcher that is set on the "eMilesDriven" variable that will make a calculation. The only other thing I can think of is that I changed my launcher icon today, but everything else in the app is working correctly so I'm not sure it is that. I just don't understand why it is running on my emulator flawlessly and crashing on my phone. I've restarted Android Studio several times, and deleted the app off my phone and tried launching it again, but everytime, it provides me with no logcat erros. I would appreciate any help I can get. If I can't get it resolved by tomorrow, I might just have to present my app on the emulator, but I would really like to know why it is crashing.

EDIT: The logcat error is as follows:

FATAL EXCEPTION: main
                                                                               Process: com.example.boley.databaseexample, PID: 15335
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.boley.databaseexample/com.example.boley.personaldeliveryassistant.OrderDetails}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                                                                                   at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:158)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                                   at com.example.boley.personaldeliveryassistant.OrderDetails.onCreate(OrderDetails.java:173)
                                                                                   at android.app.Activity.performCreate(Activity.java:6876)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                                                                                   at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:158) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method)

getCurrentFocus()

Return the view in this Window that currently has focus, or null if there are none. Note that this does not look in any containing Window.

remove getCurrentFocus() because it will return null..

what i see, you do in afterTextChanged(Editable s) which is it already point to current edittext..

other posible null pointer is here

if (ratePerMile.equals("") && !ratePerDelivery.equals(""))

change to

if (ratePerMile == null && ratePerDelivery != null)

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