2017-05-17 54 views
0

我正在爲我的移動應用程序開發最終項目開發一個android應用程序,該應用程序明天到期,並且遇到問題。直到昨天,我的應用程序才能在我的手機上正常工作,但從那以後,我不知何故弄糟了一些東西。該程序完美運行,並且在模擬器上完成了我需要的所有功能,即使在我從昨天開始添加的所有內容之後,我都認爲該應用程序已完成並準備就緒。不過,我跑了今天我的手機上,發現應用程序是在啓動下列活動崩潰......Android應用程序崩潰,沒有logcat錯誤

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

因爲昨天我已經加入的唯一的事情是對「eMilesDriven」設置TextWatcher將進行計算的變量。我唯一能想到的另一件事是,我今天改變了我的啓動器圖標,但是應用程序中的其他所有內容都正常工作,所以我不確定它是如此。我只是不明白爲什麼它在我的模擬器上完美地運行,並在我的手機上崩潰。我重新啓動了Android Studio幾次,並從手機上刪除了應用程序,並嘗試重新啓動它,但每次它都沒有提供logcat erros。我將不勝感激任何幫助。如果明天我無法解決它,我可能只需要在模擬器上展示我的應用程序,但我真的很想知道它爲什麼會崩潰。

編輯:的logcat的誤差如下:

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) 
+1

從「只顯示選定的應用程序」 - >「無過濾器」更改您的Android監視器篩選器..總是有錯誤顯示 – ZeroOne

+0

謝謝你。我不知道。我將用stacktrace更新我的問題。 –

回答

2

getCurrentFocus()

返回在此窗口當前具有焦點的視圖,或者null如果 有沒有。請注意,這不包含任何包含窗口。

刪除getCurrentFocus(),因爲它會返回null ..

我所看到的,你在afterTextChanged(Editable s)這是它已經指向當前的EditText做..

其他更多鈔票空指針是在這裏

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

變化

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

getCurrentFocus()只是爲了區分我的其他兩個TextWatcher,以防止進入無限循環。但使用你的第二個建議工作!我想讓EditTextPreference等於一個空字符串可以,但我猜不。無論如何,非常感謝你!你從字面上保存了我的成績。 –

相關問題