单击“计算”按钮时提示计算器崩溃



单击"计算"按钮时,应用程序崩溃。

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 Calculate button clicked the app crashes 
                                            totalbill.setText(Double.toString(tip_cal));
                                         }
                                     }
        );
    }
}

单击"计算"按钮时,应用程序崩溃

您需要首先从EditText获取文本。

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

应用程序由于空指针异常而崩溃,因为您没有从EditText 获取任何数据

错误在这行中

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

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

也是如此

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

tip_percent也是EditText,因此使用tip_percent.getText().toString()

最新更新