2015-11-21 39 views
-1

当Calualte按钮点击应用程序崩溃时。点击calcallate按钮时提示计算器崩溃

package thumbplay.tip; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class tip extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_tip); 
     final EditText billamt = (EditText) findViewById(R.id.tbill); 
     final EditText tip_percent = (EditText) findViewById(R.id.tpercent); 
     final TextView totalbill = (TextView) findViewById(R.id.gt); 
     Button calculate = (Button) findViewById(R.id.button); 

     calculate.setOnClickListener(new OnClickListener() { 
             public void onClick(View v) { 

              Toast.makeText(getApplicationContext(), "You have netered this ", Toast.LENGTH_SHORT).show(); 
              final double billamt1; 
              final double tip_percent1; 
              final double totalbill1; 
              final double tip_cal; 


              billamt1 = Double.parseDouble(billamt.toString()); 
              tip_percent1 = Double.parseDouble(tip_percent.toString()); 
              tip_cal= (billamt1 * tip_percent1)/100; //When Calualte button clicked the app crashes 
              totalbill.setText(Double.toString(tip_cal)); 

             } 
            } 
     ); 


    } 


} 

当Calualte按钮点击应用程序崩溃,我在andoroid编程新的,请帮我解决这个问题,谢谢!

+0

您可以发布错误日志? – kiturk3

回答

2

您需要先获取EditText的文字。

billamt1=Double.parseDouble(billamt.gettext().toString()); 
tip_percent1=Double.parseDouble(tip_percent.gettext().toString()); 

该应用程序崩溃是由于空指针异常,因为你不是从EditText

1

错误得到任何数据是在这一行

billamt1 = Double.parseDouble(billamt.toString()); 

这里billamt是你EditText,如果你想要它的价值,你应该使用billamt.getText().toString()

同样的事情去

tip_percent1 = Double.parseDouble(tip_percent.toString()); 

tip_percentEditText所以使用tip_percent.getText().toString()