Android:获取数字,进行计算,显示结果

时间:2015-01-31 21:08:46

标签: android android-studio compiler-errors onclicklistener

已更新

现在应用程序启动了,但是当我按下" Calcular"按钮应用程序中断,并给我一个错误。

这是代码:

package com.example.casa.gasquefabaixada;

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

import static java.lang.Math.E;
import static java.lang.Math.asin;
import static java.lang.Math.cos;
import static java.lang.Math.log;
import static java.lang.Math.pow;
import static java.lang.Math.sin;
import static java.lang.Math.sqrt;
import static java.lang.Math.tanh;

public class MainActivity extends Activity {
EditText denEditText, gEditText, mEditText, sEditText, cEditText, muEditText, xEditText, HiniEditText, HfiEditText, veliniEditText;
double den;
double g;
double m;
double s;
double c;
double mu;
double x;
double Hini;
double Hfi;
double velini;
double temps;
double velKm;
double velMaxKm;

public MainActivity() {
}

double arctanh(double x)
{
    return (0.5*(log(1+x)-log(1-x)));
}
double arccosh(double y)
{
    return log(y + sqrt(y * y -1));
}
double a(double g, double an, double mu)
{
    return sqrt(g*sin(an)-mu*g*cos(an));
}

double angle(double hini, double hfi, double x)
{
    return asin((hini-hfi)/x);
}
double b(double den, double s, double c, double m)
{
    return sqrt(den*s*c/(2*m));
}
double k(double v, double a, double b)
{
    return arctanh(v*b/a);
}
double temps(double a, double b, double x, double k)
{
    double y=pow(E,x*b*b);
    return (( arccosh(y)-k)/(b*a)); //-4 es per ajustar
}
double vel(double a,double b,double t, double k)
{
    return (a*tanh(a*b*t+k)/b);
}
double velmax(double a, double b)
{
    return (a/b);
}
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button calcular = (Button) findViewById(R.id.calcular);

    calcular.setOnClickListener(OkListener);

}

private OnClickListener OkListener =new OnClickListener(){
    public void onClick(View view)
    {
        den = Double.parseDouble(denEditText.toString());
        g = Double.parseDouble(gEditText.toString());
        m = Double.parseDouble(mEditText.toString());
        s = Double.parseDouble(sEditText.toString());
        c = Double.parseDouble(cEditText.toString());
        mu = Double.parseDouble(muEditText.toString());
        x = Double.parseDouble(xEditText.toString());
        Hini = Double.parseDouble(HiniEditText.toString());
        Hfi = Double.parseDouble(HfiEditText.toString());
        velini = Double.parseDouble(veliniEditText.toString());

        double angle=angle(Hini,Hfi,x);
        double a=a(g,angle,mu);
        double b=b(den,s,c,m);
        double k=k(velini,a,b);
        double temps=temps(a,b,x,k);
        double vel=vel(a,b,temps,k);
        double velMax=velmax(a,b);
        double velKm=vel*3.6;
        double velMaxKm=velMax*3.6;



    }
};

public void calculate(View view) {
    TextView tv = (TextView) findViewById(R.id.velocitat);
    tv.setText(String.valueOf(velMaxKm));

    TextView ot = (TextView) findViewById(R.id.circ);
    ot.setText(String.valueOf(velKm));

    TextView mm = (TextView) findViewById(R.id.pep);
    mm.setText(String.valueOf(temps));
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

这是日志错误:

Process: com.example.casa.gasquefabaixada, PID: 3961
    java.lang.NullPointerException: Attempt to invoke virtual method     'java.lang.String android.view.View.toString()' on a null object reference
            at     com.example.casa.gasquefabaixada.MainActivity$1.onClick(MainActivity.java:94)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com .android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-01 00:01:13.711    5317-5352/com.example.casa.gasquefabaixada     D/OpenGLRenderer﹕ Render dirty regions requested: true
02-01 00:01:13.720    5317-5317/com.example.casa.gasquefabaixada D/Atlas﹕     Validating map...
02-01 00:01:13.836    5317-5352/com.example.casa.gasquefabaixada I/Adreno-    EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c,     Ia6306ec328
02-01 00:01:13.838    5317-5352/com.example.casa.gasquefabaixada      I/OpenGLRenderer﹕ Initialized EGL, version 1.4
02-01 00:01:13.876    5317-5352/com.example.casa.gasquefabaixada     D/OpenGLRenderer﹕ Enabling debug mode 0
02-01 00:02:19.483    5317-5317/com.example.casa.gasquefabaixada     D/AndroidRuntime﹕ Shutting down VM
02-01 00:02:19.489    5317-5317/com.example.casa.gasquefabaixada     E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.casa.gasquefabaixada, PID: 5317
    java.lang.NullPointerException: Attempt to invoke virtual method     'java.lang.String android.view.View.toString()' on a null object reference
            at     com.example.casa.gasquefabaixada.MainActivity$1.onClick(MainActivity.java:94)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-01 00:02:21.645    5317-5317/com.example.casa.gasquefabaixada I/Process﹕     Sending signal. PID: 5317 SIG: 9

PD:我使用数字(十进制)框来输入和输出数字。

3 个答案:

答案 0 :(得分:2)

您缺少一些进口产品。例如     导入android.view.View.OnClickListener缺失。

按Ctr + Shift + O自动组织导入。

答案 1 :(得分:2)

首先,你需要替换其中的每一个:

    den= (Double) findViewById(R.id.editText);
    g=(Double) findViewById(R.id.editText2);
    m=(Double) findViewById(R.id.editText3);
    s=(Double) findViewById(R.id.editText4);
    c=(Double) findViewById(R.id.editText5);
    mu=(Double) findViewById(R.id.editText6);
    x=(Double) findViewById(R.id.editText7);
    Hini=(Double) findViewById(R.id.editText8);
    Hfi=(Double) findViewById(R.id.editText9);
    velini=(Double) findViewById(R.id.editText10);

以下内容:

    denEditText= (EditText) findViewById(R.id.editText);
    gEditText=(EditText) findViewById(R.id.editText2);
    mEditText=(EditText) findViewById(R.id.editText3);
    sEditText=(EditText) findViewById(R.id.editText4);
    cEditText=(EditText) findViewById(R.id.editText5);
    muEditText=(EditText) findViewById(R.id.editText6);
    xEditText=(EditText) findViewById(R.id.editText7);
    HiniEditText=(EditText) findViewById(R.id.editText8);
    HfiEditText=(EditText) findViewById(R.id.editText9);
    veliniEditText=(EditText) findViewById(R.id.editText10);

还要在变量定义中添加以下内容:

EditText denEditText, gEditText, mEditText, sEditText, cEditText, muEditText, xEditText, HiniEditText, HfiEditText, veliniEditText

最后,在计算之前,您需要将EditText的值设置为变量:

den = Double.parseDouble(denEditText.getText()); 
g = Double.parseDouble(gEditText.getText());
m = Double.parseDouble(mEditText.getText());
s = Double.parseDouble(sEditText.getText());
c = Double.parseDouble(cEditText.getText());
mu = Double.parseDouble(muEditText.getText());
x = Double.parseDouble(xEditText.getText());
Hini = Double.parseDouble(HiniEditText.getText());
Hfi = Double.parseDouble(HfiEditText.getText());
velini = Double.parseDouble(veliniEditText.getText());

您可能也会错过一些其他人建议的导入

答案 2 :(得分:0)

您可以在Button xml代码中设置onClick属性,如下所示:android:onClick="calculate"

然后在java代码中创建一个新方法

public void calculate(View view) {
    TextView tv = (TextView) findViewById(R.id.YourTextViewId);
    tv.setText("Temps = " + Temps + ", " +  "Vel = " + Vel + ", " + "VelMax = " +  VelMax );
}

这将在上面的TextField中显示您的变量值。 如果您仍然遇到任何问题,请在下面注明具体问题。