我在下面粘贴了一些代码。我正在尝试使用此按钮激活下面的计算并将结果放在EditText视图中。我发现程序中没有列出任何错误,但是当我按下按钮(calcbutton)时,它在我的Android手机上崩溃了。
我想知道我的BigDecimal计算是否有问题,但我不知道为什么没有报告错误。
public String massVal;
public String volVal;
public String volunitdes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sfp);
//declare ListView
final ListView mass;
//get ListView object from xml
mass = (ListView) findViewById(R.id.mass);
//defined array values to show in ListView
String[] masslv = new String[]{"g", "mg", "µg", "mol", "mmol", "µmol"};
//define new Adapter
ArrayAdapter<String> massAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, android.R.id.text1, masslv);
//assign Adapter to ListView
mass.setAdapter(massAdapter);
//ListView Item Click Listener
mass.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//ListView Clicked item index
int itemPosition = position;
//ListView Clicked item value
String massVal = (String) mass.getItemAtPosition(position);
//declare EditText for mass and place clicked value (String massVal) into box
EditText masstext;
masstext = (EditText) findViewById(R.id.masstext);
masstext.setText(massVal);
}});
//button for final calculation
Button calcbutton;
calcbutton = (Button) findViewById(R.id.calcbutton);
calcbutton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//declare all manual number entries as EditText, then String, then BigDecimal
EditText CONCdes = (EditText) findViewById(R.id.CONCdes);
EditText voldes = (EditText) findViewById(R.id.voldes);
EditText MWbox = (EditText) findViewById(R.id.MWbox);
String CONCVAL = CONCdes.getText().toString();
String VOLDES = voldes.getText().toString();
String MW = MWbox.getText().toString();
BigDecimal CONCVALbd = new BigDecimal(CONCVAL).setScale(2, RoundingMode.HALF_UP);
BigDecimal VOLDESbd = new BigDecimal(VOLDES).setScale(2, RoundingMode.HALF_UP);
BigDecimal MWbd = new BigDecimal(MW).setScale(2, RoundingMode.HALF_UP);
//default calculation assumes ug/ml, ml vol units to measure, and mg units to weigh
BigDecimal FINPOWDMEAS;
FINPOWDMEAS = VOLDESbd.multiply(CONCVALbd).divide(BigDecimal.valueOf(1000)).setScale(2, RoundingMode.HALF_UP);
if (massVal.equals("g"))
{FINPOWDMEAS = FINPOWDMEAS.multiply(BigDecimal.valueOf(1000000)).setScale(2, RoundingMode.HALF_UP);}
if (massVal.equals("mg"))
{FINPOWDMEAS = FINPOWDMEAS.multiply(BigDecimal.valueOf(1000));}
if (massVal.equals("mol"))
{FINPOWDMEAS = VOLDESbd.multiply(CONCVALbd).multiply(MWbd).multiply(BigDecimal.valueOf(1000)).setScale(2, RoundingMode.HALF_UP);}
if (massVal.equals("mmol"))
{FINPOWDMEAS = (VOLDESbd.multiply(CONCVALbd).divide(BigDecimal.valueOf(1000))).multiply(MWbd).multiply(BigDecimal.valueOf(1000)).setScale(2, RoundingMode.HALF_UP);
if (massVal.equals("µmol"))
{FINPOWDMEAS = (VOLDESbd.multiply(CONCVALbd).divide(BigDecimal.valueOf(1000000))).multiply(MWbd).multiply(BigDecimal.valueOf(1000)).setScale(2, RoundingMode.HALF_UP);}
if (volVal.equals("L"))
{FINPOWDMEAS = FINPOWDMEAS.divide(BigDecimal.valueOf(1000)).setScale(2, RoundingMode.HALF_UP);}
if (volVal.equals("dL"))
{FINPOWDMEAS = FINPOWDMEAS.divide(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP);}
if (volVal.equals("µL")){FINPOWDMEAS = FINPOWDMEAS.multiply(BigDecimal.valueOf(1000)).setScale(2, RoundingMode.HALF_UP);}
//final calculation value to string
EditText FINPOWDMEASURE = (EditText) findViewById(R.id.FINPOWDMEASURE);
String FINALPOWDERMEASURE = FINPOWDMEAS.toString();
FINPOWDMEASURE.setText(FINALPOWDERMEASURE);}});
例外:
Process: vanwert.sdb, PID: 907
java.lang.NullPointerException
at vanwert.sdb.SFP$4.onClick(SFP.java:134)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
该行
String massVal = (String) mass.getItemAtPosition(position);
让您将值分配给名为massVal
的新变量,其范围与先前声明的massVal
不同。
切换到:
massVal = (String) mass.getItemAtPosition(position);
答案 1 :(得分:0)
有一些问题。
在这里宣布一个班级成员massVal
很好。
public String massVal;
然后重新声明并在匿名OnItemClickListener
中分配 massVal。
String massVal = (String) mass.getItemAtPosition(position);
在提供此问题的匿名OnClickListener
中,与其上方的匿名OnItemClickListener
的范围完全不同,massVal永远不会被分配,因此仍为null
。
通常更好的设计是这样的:
public class SomeActivity extends Activity {
private Button myButton;
private EditText myEditText;
private String myString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main.xml);
myEditText = (EditText) findViewById(R.id.my_edit_text);
myButton = (Button) findViewById(R.id.my_button);
myButton.setOnClickListener(new View.OnClickListener() {
String text = myEditText.getText()
if (text != null) {
myString = text.toString();
// ... continue processing your string
}
});
}
}