Android应用在计算时崩溃

时间:2014-12-20 20:23:24

标签: java android

当我尝试计算某些内容时,我的应用总是崩溃。我查看了logcat,发现它抛出了NullPointerException。也试过调试,但没有找到出路。这是我的代码:

Consumer.java

public class ConsumerMath  {
public double discount;
public double discountRate;
public double salesPrice;
public double listPrice;



public double getDiscount() {
    return discount;
}
public void setDiscount(double discount) {
    this.discount = discount;
}
public double getDiscountRate() {
    return discountRate;
}
public void setDiscountRate(double discountRate) {
    this.discountRate = discountRate;
}
public double getSalesPrice() {
    return salesPrice;
}
public void setSalesPrice(double salesPrice) {
    this.salesPrice = salesPrice;
}
public double getListPrice() {
    return listPrice;
}
public void setListPrice(double listPrice) {
    this.listPrice = listPrice;
}
public double discount(){
    return listPrice*discountRate;  
}
/*public double discount(double listPrice, double discountRate){
    return listPrice*discountRate;  
}*/
public double salesPrice(){
    return listPrice - discount;
}
public double discountRate(){
    return discount / listPrice;
}

}

MainActivity.java

package com.example.mathshelp;

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

import com.example.formula.ConsumerMath;

public class MainActivity extends Activity implements OnClickListener {
    EditText input;
    EditText input2;
    EditText output;
    Button ok;
    public static ConsumerMath mc;
    double value;
    double value1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        input = (EditText) findViewById(R.id.firstparam);
        input2 = (EditText) findViewById(R.id.secondparam);
        output = (EditText) findViewById(R.id.lasttparam);
        ok = (Button) findViewById(R.id.OK);

        try{
            ok.setOnClickListener(this);

        }
        catch(Exception e){

        }

    }


    @Override
    public void onClick(View v) {
        //m.listPrice 
        //value = Long.parseLong(input.getText().toString());
        mc.setListPrice(Long.parseLong(input.getText().toString()));
        //m.listPrice = Long.parseLong(input.getText().toString());
        //value1 = Long.parseLong(input2.getText().toString());
        mc.setDiscountRate(Long.parseLong(input2.getText().toString()));
        //m.discountRate = Long.parseLong(input.getText().toString());

        StringBuilder facto1 = new StringBuilder();

            double d  = 0;
            //d = m.discount(m.listPrice, m.discount);
            d = mc.discount();

                    //d = m.listPrice*m.discount;

            facto1.append(d);

            output.setText(facto1);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SaveFileXML" >

<TextView
    android:id="@+id/save_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerInParent="true"
    android:textSize="35sp"
    android:textStyle="bold"
    android:text="Something" />

<EditText
    android:id="@+id/secondparam"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/firstparam"
    android:hint="@string/hint"
    android:paddingTop="50dp" />

<EditText
    android:id="@+id/lasttparam"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/secondparam"
    android:hint="@string/hint"
    android:paddingTop="50dp" />


<Button
    android:id="@+id/OK"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="64dp"
    android:text="Ok" />

<EditText
    android:id="@+id/firstparam"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/save_textview"
    android:ems="10"
    android:hint="@string/hint"
    android:paddingTop="50dp" >

    <requestFocus />
</EditText>

0 个答案:

没有答案