- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将一个数值放入 EditText
中。假设我得到输入首先说“输入”。然后对输入执行toString()
。然后parseDouble()
。假设结果是输入*2。所以我然后在 EditText
中进行设置。假设,
package com.hasibshovon.unitconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
// data source
private static String[] types = { "Angle", "Area", "Bits&Bytes", "Density",
"Electric Current", "Energy", "Force", "Length", "Mass", "Power",
"Pressure", "Speed", "Temperature", "Time", "Volume" };
private static String[] AngleUnits = { "Degree", "Radian", "Grad", "Gon",
"Minute", "Second", "SIgn", "Mil", "Revolution", "Circle", "Turn",
"Quadrant", "Right", "Triangle", "Sextant" };
private static String[] AreaUnits = { "SquareMeter", "SquareKilometer",
"SquareHectometer", "SquareDekameter", "SquareDecimeter",
"SquareCentimeter", "SquareMilimeter", "SquareMicrometer",
"SquareNanometer", "Hectare", "SquareMile", "SquareYard",
"SquareFoot", "SquareInch", "Acre" };
private static String[] BitsBytesUnits = { "bit", "byte", "kilobit",
"kilobyte", "megabit", "megabyte", "gigabit", "gigabyte",
"terabit", "terabyte", "petabit", "petabyte", "exabit", "exabyte" };
private static String[] DensityUnits = { "kilogram/cubicmeter",
"kilogram/cubiccentimeter", "gram/cubicmeter",
"gram/cubiccentimeter", "gram/cubicmilimeter",
"miligram/cubicmeter", "miligram/cubiccentimeter",
"miligram/cubicmilimeter", "exagram/liter", "petagram/liter",
"teragram/liter", "gigagram/liter", "meggram/liter",
"kilogram/liter", "hectogram/liter", "decagram/liter",
"gram/liter", "decigram/liter", "centigram/liter",
"miligram/liter", "microgram/liter", "nanogram/liter",
"picogram/liter" };
private static String[] CurrentUnits = { "ampere", "kiloampere",
"miliampere", "biot", "abaampere", "stataampere" };
private static String[] EnergyUnits = { "joule", "gigajoule", "megajoule",
"kilojoule", "milijoule", "microjoule", "nanojoule", "attojoule" };
private static String[] ForceUnits = { "newton", "dyne", "joul/meter",
"joul/centimeter", "gram-force", "kilogram-force", "kip-force",
"pond", "kilo-pond" };
private static String[] LengthUnits = { "meter", "exameter", "terameter",
"petameter", "gigameter", "megameter", "kilometer", "hectometer",
"dekameter", "decimeter", "centimeter", "milimeter", "micrometer",
"nanometer", "picometer", "femtometer", "attometer" };
private static String[] MassUnits = { "gram", "kilogram", "miligram",
"dalton", "pound", "ounce", "ton", "quarter", "grain" };
private static String[] PowerUnits = { "watt", "megawatt", "horsepower",
"kilocalorie", "joul/hour", "kilojoul/hour", "joul/second" };
private static String[] PressureUnits = { "pascal", "newton/squaremeter",
"bar", "kilogram-force/squaremeter", "ton-force/squarefoot",
"poundal/squarefoot", "atmosphere(atm)" };
private static String[] SpeedUnits = { "meter/second", "kilometer/hour",
"foot/hour", "yard/hour", "mile/hour", "mile/second", "knot" };
private static String[] TemperatureUnits = { "kelvin", "celsius",
"farenheit", "rankine", "reaumur" };
private static String[] TimeUnits = { "second", "milisecond",
"microsecond", "nanosecond", "minute", "hour", "day", "week",
"month", "year", "decade", "century", "millenium" };
private static String[] VolumeUnits = { "cubicmeter", "cubickilometer",
"cubiccentimeter", "liter", "cc", "barrel", "cup", "cubicmile",
"cubicfoot", "cubicyard", "cubicinch", "acre-foot" };
private Spinner spnUnitTypes;
private Spinner spnFromUnit;
private Spinner spnToUnit;
private EditText etFrom;
private EditText etTo;
public String value1, value2;
public double fromDouble;
public double result;
// adapters
ArrayAdapter<String> adaptUnittype;
ArrayAdapter<String> adaptFrom;
ArrayAdapter<String> adaptTo;
// private OnItemSelectedListener UnitLisetener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// spnUnitTypes.setOnItemSelectedListener(UnitLisetener);
etFrom = (EditText) findViewById(R.id.etConvertFrom);
etTo = (EditText) findViewById(R.id.etToResult);
// initialize view
spnUnitTypes = (Spinner) findViewById(R.id.spnUnitType);
spnFromUnit = (Spinner) findViewById(R.id.spnFromUnit);
spnToUnit = (Spinner) findViewById(R.id.spnToUnit);
// initialize adapter
adaptUnittype = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, types);
// bind adapter and view
spnUnitTypes.setAdapter(adaptUnittype);
spnUnitTypes.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int postion, long arg3) {
if (types[postion].equals("Time")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, TimeUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, TimeUnits);
} else if (types[postion].equals("Mass")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, MassUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, MassUnits);
} else if (types[postion].equals("Angle")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AngleUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AngleUnits);
} else if (types[postion].equals("Area")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AreaUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, AreaUnits);
} else if (types[postion].equals("Density")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, DensityUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, DensityUnits);
} else if (types[postion].equals("Bits&Bytes")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
BitsBytesUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
BitsBytesUnits);
} else if (types[postion].equals("Electric Current")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, CurrentUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, CurrentUnits);
} else if (types[postion].equals("Energy")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, EnergyUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, EnergyUnits);
} else if (types[postion].equals("Force")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, ForceUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, ForceUnits);
} else if (types[postion].equals("Length")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, LengthUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, LengthUnits);
} else if (types[postion].equals("Power")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PowerUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PowerUnits);
} else if (types[postion].equals("Pressure")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PressureUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, PressureUnits);
} else if (types[postion].equals("Speed")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, SpeedUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, SpeedUnits);
} else if (types[postion].equals("Temperature")) {
adaptFrom = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
TemperatureUnits);
adaptTo = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
TemperatureUnits);
}
spnFromUnit.setAdapter(adaptFrom);
spnToUnit.setAdapter(adaptTo);
spnFromUnit
.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View arg1, int pos, long arg3) {
Object item = parent.getItemAtPosition(pos);
value1 = item.toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spnToUnit
.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View arg1, int pos, long arg3) {
Object item1 = parent.getItemAtPosition(pos);
value2 = item1.toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
String fromstr = etFrom.getText().toString();
double fromDouble = Double.parseDouble(fromstr);
}
public void convert(View v) {
if (value1.equals("Degree") && value2.equals("Radian")) {
etTo.setText(String.valueOf(fromDouble*2));
}
}
@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;
}
}
最佳答案
您的输入似乎为空或不包含要解析的数字。通过使用 try/catch 记录或打印出值来尝试调试
String fromstr = etFrom.getText().toString();
try {
double fromDouble = Double.parseDouble(fromstr);
} catch (NumberFormatException e) {
System.out.println(fromstr)
}
关于java - 获取 numberFormatException 无效 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21886516/
下面的代码有效,我觉得double(double)和double(*)(double)没有区别,square和 &square,我说得对吗? #include double square(doubl
我知道我的作业很草率,这是我在这门课上的第 4 次作业。任何帮助将不胜感激,谢谢。 double getPrincipal(0); double getRate(0); double getYe
我遇到了那个错误,当我使用类时,我在使用函数指针时遇到了这个错误。我的函数'ope'函数我该如何解决 evaluator::function(){ double (*ope) (dou
问题://故事从哪里开始 Graphics 类型中的方法 drawLine(int, int, int, int) 不适用于参数 (double, double, double, double) g.
我有一张 map> m1 形式的 map .我可以将其复制到 map m2 形式的 map 吗?这样键是相同的,并且 m2 中的值是 get(m1->second) 不使用循环?谢谢! 最佳答案 这样
有没有办法获取vector> 的“.first”和“.second”的连续内存? ?我的意思是: void func(int N, double* x, double* y) { for (i
我正在尝试将自定义 lambda 传递给需要函数指针的函数(更准确地说是 zero 中的 Brent library 函数)。 我的想法是,我将使用参数创建一次 lambda,然后用多个值对其求值 x
这是一个很简单的问题,让我很困惑。 我收到一个源文件的以下错误,但另一个没有: 4 src/Source2.cpp:1466: error: no matching function for cal
struct CalculatorBrain { private var accumulator: Double? func changeSign(operand: Double) -
在我正在进行的项目中,我尝试使用 curlpp库来发出一个简单的 html GET 请求。当我将 cpp 文件传递给 g++ 时,出现以下错误: /usr/local/include/curlpp
不使用double就能获得quadruple精度超过16位的数字吗?如果可能的话,这取决于编译器还是其他?因为我知道有人说他使用double精度,并且具有22位精度。 最佳答案 数据类型double
我正在寻找有关特斯拉 GPU 中硬件如何实现 double 的信息。我读到,两个流处理器正在处理单个 double 值,但我没有找到 nvidia 的任何官方论文。 提前致谢。聚苯硫醚为什么大多数 G
这个问题在这里已经有了答案: Passing capturing lambda as function pointer (10 个答案) 关闭 2 年前。 我有这个错误 error: cannot
情况:我有一个元组列表,其中添加了一个元组: List> list = new List>(); list .Add(new Tuple(2.2, 6.6)); 一切似乎都还好。但是......在 D
我有一个 JList,里面有一堆名字,还有一个包含这些名字值的数组 final Double[] filmcost = { 5.00, 5.50, 7.00, 6.00, 5.00 }; 我想做的是,
我试图找出牛顿法来求方程的根。这个错误出来了,我无法处理。 double fn(double n){ return sin(n)+log(n)-1; } double f1n(double n
我有一个 junit 测试断言两个 Double 对象,具有以下内容: Assert.assertEquals(Double expected, Double result); 这很好,然后我决定将其
我正在尝试引入部分数据文件来填充数组,用户尝试了三次输入正确的数据文件名。我一再遇到这些错误。我知道像 arr 这样的数组只是一个指向内存块的指针。 #include #include #incl
我正在尝试完成复习题(为即将到来的编程决赛),但是,我无法解决这个问题,因为我不断收到错误(标题)。正如预期的那样,我将发布问题和我尝试的解决方案。 问题: 给定以下函数定义:void swap(do
任何人都知道如何实现这一目标。我已经尝试了通常的公式,但我只得到正数 Double.NEGATIVE_INFINITY) return d; } } 这将以相同的概率
我是一名优秀的程序员,十分优秀!