gpt4 book ai didi

android - Android : Take numbers, do calculations, show result

转载 作者:行者123 更新时间:2023-12-02 10:49:49 25 4
gpt4 key购买 nike

更新

现在该应用程序启动,但是当我按“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:我使用数字(十进制)框输入和输出数字。

最佳答案

首先,您需要替换以下各项:

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

您可能还会错过其他人建议的某些进口商品

关于android - Android : Take numbers, do calculations, show result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28256547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com