A picture of the Error is right here
由于某种原因,我无法从EditText解析双打。 每次我输入一个数字时,EditText都无法获取我键入的内容,但是当我输入字母时,它可以感觉到我输入了字母(两次都仍然抛出了NumberFormatException。
这是相关代码。
Java
public class rLawCalc extends Fragment
{
View rootview;
Spinner spin;
TextView trial;
TextView r1;
TextView r2;
TextView r3;
TextView r4;
TextView A;
TextView B;
TextView C;
TextView D;
TextView Answer;
EditText A1;
EditText A2;
EditText A3;
EditText A4;
EditText B1;
EditText B2;
EditText B3;
EditText B4;
EditText C1;
EditText C2;
EditText C3;
EditText C4;
EditText D1;
EditText D2;
EditText D3;
EditText D4;
Button GO;
double concA1 = 0;
double concA2 = 0;
double concA3 = 0;
double concB1 = 0;
double concB2 = 0;
double concB3 = 0;
double concC1 = 0;
double concC2 = 0;
double concC3 = 0;
double concD1 = 0;
double concD2 = 0;
double concD3 = 0;
double rateA = 0;
double rateB = 0;
double rateC = 0;
double concA = 0;
double concB = 0;
double concC = 0;
double rate1 = 0;
double rate2 = 0;
double rate3 = 0;
double rate4 = 0;
int expA;
int expB;
int expC;
boolean onClick = false;
String resp;
String spinner = "";
ArrayAdapter<CharSequence> choices;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//inflate rootview
rootview = inflater.inflate(R.layout.fragment_r_law_calc, container, false);
//load EditText
A1 = (EditText) rootview.findViewById(R.id.A1);
A1.setVisibility(View.GONE);
A2 = (EditText) rootview.findViewById(R.id.A2);
A2.setVisibility(View.GONE);
A3 = (EditText) rootview.findViewById(R.id.A3);
A3.setVisibility(View.GONE);
A4 = (EditText) rootview.findViewById(R.id.A4);
A4.setVisibility(View.GONE);
B1 = (EditText) rootview.findViewById(R.id.B1);
B1.setVisibility(View.GONE);
B2 = (EditText) rootview.findViewById(R.id.B2);
B2.setVisibility(View.GONE);
B3 = (EditText) rootview.findViewById(R.id.B3);
B3.setVisibility(View.GONE);
B4 = (EditText) rootview.findViewById(R.id.B4);
B4.setVisibility(View.GONE);
C1 = (EditText) rootview.findViewById(R.id.C1);
C1.setVisibility(View.GONE);
C2 = (EditText) rootview.findViewById(R.id.C2);
C2.setVisibility(View.GONE);
C3 = (EditText) rootview.findViewById(R.id.C3);
C3.setVisibility(View.GONE);
C4 = (EditText) rootview.findViewById(R.id.C4);
C4.setVisibility(View.GONE);
D1 = (EditText) rootview.findViewById(R.id.D1);
D1.setVisibility(View.GONE);
D2 = (EditText) rootview.findViewById(R.id.D2);
D2.setVisibility(View.GONE);
D3 = (EditText) rootview.findViewById(R.id.D3);
D3.setVisibility(View.GONE);
D4 = (EditText) rootview.findViewById(R.id.D4);
D4.setVisibility(View.GONE);
//load spinner
spin = (Spinner) rootview.findViewById(R.id.numReactants);
choices = ArrayAdapter.createFromResource( getActivity(), R.array.numOfReactants, android.R.layout.simple_spinner_item);
choices.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(choices);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Made to Test Spinner
// Toast.makeText(getActivity(), parent.getItemAtPosition(position) + " is selected", Toast.LENGTH_LONG).show();
spinner = "" + parent.getItemAtPosition(position);
switch (spinner)
{
case "1":
trial = (TextView) rootview.findViewById(R.id.Trial);
trial.setText(R.string.t1);
r1 = (TextView) rootview.findViewById(R.id.tv1);
r1.setText("1");
r2 = (TextView) rootview.findViewById(R.id.tv2);
r2.setText("2");
A = (TextView) rootview.findViewById(R.id.concA);
A.setText(" [A]");
B = (TextView) rootview.findViewById(R.id.concB);
B.setText(R.string.rxnrate);
B.setTextSize(15);
A1.setVisibility(View.VISIBLE);
A2.setVisibility(View.VISIBLE);
B1.setVisibility(View.VISIBLE);
B2.setVisibility(View.VISIBLE);
GO.setVisibility(View.VISIBLE);
break;
case "2":
trial = (TextView) rootview.findViewById(R.id.Trial);
trial.setText(R.string.t1);
r1 = (TextView) rootview.findViewById(R.id.tv1);
r1.setText("1");
r2 = (TextView) rootview.findViewById(R.id.tv2);
r2.setText("2");
A = (TextView) rootview.findViewById(R.id.concA);
A.setText("[A]");
B = (TextView) rootview.findViewById(R.id.concB);
B.setText("[B]");
C = (TextView) rootview.findViewById(R.id.concC);
C.setText(R.string.rxnrate);
A1.setVisibility(View.VISIBLE);
A2.setVisibility(View.VISIBLE);
A3.setVisibility(View.VISIBLE);
B1.setVisibility(View.VISIBLE);
B2.setVisibility(View.VISIBLE);
B3.setVisibility(View.VISIBLE);
C1.setVisibility(View.VISIBLE);
C2.setVisibility(View.VISIBLE);
C3.setVisibility(View.VISIBLE);
GO.setVisibility(View.VISIBLE);
break;
case"3":
trial = (TextView) rootview.findViewById(R.id.Trial);
trial.setText(R.string.t1);
r1 = (TextView) rootview.findViewById(R.id.tv1);
r1.setText("1");
r2 = (TextView) rootview.findViewById(R.id.tv2);
r2.setText("2");
r3 = (TextView) rootview.findViewById(R.id.tv3);
r3.setText("3");
r4 = (TextView) rootview.findViewById(R.id.tv4);
r4.setText("4");
A = (TextView) rootview.findViewById(R.id.concA);
A.setText("[A]");
B = (TextView) rootview.findViewById(R.id.concB);
B.setText("[B]");
C = (TextView) rootview.findViewById(R.id.concC);
C.setText("[C]");
D = (TextView) rootview.findViewById(R.id.concD);
D.setText(R.string.rxnrate);
A1.setVisibility(View.VISIBLE);
A2.setVisibility(View.VISIBLE);
A3.setVisibility(View.VISIBLE);
A4.setVisibility(View.VISIBLE);
B1.setVisibility(View.VISIBLE);
B2.setVisibility(View.VISIBLE);
B3.setVisibility(View.VISIBLE);
B4.setVisibility(View.VISIBLE);
C1.setVisibility(View.VISIBLE);
C2.setVisibility(View.VISIBLE);
C3.setVisibility(View.VISIBLE);
C4.setVisibility(View.VISIBLE);
D1.setVisibility(View.VISIBLE);
D2.setVisibility(View.VISIBLE);
D3.setVisibility(View.VISIBLE);
D4.setVisibility(View.VISIBLE);
GO.setVisibility(View.VISIBLE);
break;
}
}
public void onNothingSelected(AdapterView<?> parent)
{
Toast.makeText(getActivity(), "Please select an Option", Toast.LENGTH_LONG).show();
}
});
//load Button
GO = (Button) rootview.findViewById(R.id.GO);
GO.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
switch(spinner)
{
case "1":
case1Ints();
Answer = (TextView) rootview.findViewById(R.id.Answer);
Answer.setText(resp);
break;
case "2":
case2Ints();
Answer = (TextView) rootview.findViewById(R.id.Answer);
Answer.setText(resp);
break;
case "3":
case3Ints();
Answer = (TextView) rootview.findViewById(R.id.Answer);
Answer.setText(resp);
}
}
});
GO.setVisibility(View.GONE);
return rootview;
}
public void case1Ints()
{
try
{
concA1=Double.parseDouble(A1.getText().toString().replaceAll("\\s+",""));
concA2 = Double.parseDouble(B1.getText().toString().replaceAll("\\s+",""));
rate1 = Double.parseDouble(A3.getText().toString().replaceAll("\\s+",""));
rate2 = Double.parseDouble(B3.getText().toString().replaceAll("\\s+",""));
}
catch(NumberFormatException n)
{
Toast.makeText(getActivity(), "Please enter numbers only", Toast.LENGTH_LONG).show();
}
double temp = concA2/concA1;
double rate_change = rate2/rate1;
if(rate_change == temp)
{
resp = "Rate = k[A]";
}
else if(rate_change == temp*temp)
{
resp = "Rate = k[A]2";
resp = resp.replaceAll("2", "²");
}
else if(rate_change == 1)
{
resp = "Rate = k";
}
//have to make everything within an acceptable range
}
答案 0 :(得分:1)
我一直遵循的代码是,这些总是帮助我创建科学计算器,我总是在我的代码中使用这些,
EditText refer=(EditText)findViewById(R.id.idofet);
double val=Double.parseDouble(refer.getText().toString());
希望它会对你有所帮助。
答案 1 :(得分:0)
如果键入数字而没有数字点,Java可以将其视为整数。所以,在你调用parseDouble之前,我建议你先将它转换为Integer。喜欢:(double)
Integer.parseInt(A1.getText().toString().replaceAll("\\s+",""));
如果要检测字符更改,还需要添加onTextChanged侦听器。它适用于您键入的任何字符。
A1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i,
int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1,
int i2) {
// Here you could parse the entered text
}
@Override
public void afterTextChanged(Editable editable) {
}
});
答案 2 :(得分:0)
我有一些建议,你可以尝试:
A1.getText().toString().replaceAll("\\s+","");
的内容,例如查看它是否反映了您尝试解析的数字。 答案 3 :(得分:0)
更新
要看到这个,请注释抛出错误并制作错误的代码 TextView并打印getText()的返回结果。