我正在使用整数并尝试进行涉及零件小数的计算。我之前使用过相同的设置并且它已经工作但是由于某种原因这不起作用。
package table.periodic;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class Gram2mol extends Activity implements AdapterView.OnItemSelectedListener {
private Spinner Spinner01;
private TextView elementnumber;
private EditText grams;
private Button calculate;
private TextView moles5;
String[] element={"Actinium", "Aluminum", "Americium",
"Antimony", "Argon", "Arsenic", "Astatine", "Barium",
"Berkelium", "Beryllium", "Bismuth", "Bohrium", "Boron",
"Bromine", "Cadmium", "Calcium", "Californium", "Carbon",
"Cerium", "Cesium", "Chlorine", "Chromium", "Cobalt",
"Copernicium", "Copper", "Curium", "Darmstadtium", "Dubnium",
"Dysprosium", "Einsteinium", "Erbium", "Europium", "Fermium",
"Fluorine", "Francium", "Gadolinium", "Gallium", "Germanium",
"Gold", "Hafnium", "Hassium", "Helium", "Holmium",
"Hydrogen", "Indium", "Iodine", "Iridium", "Iron",
"Krypton", "Lanthanum", "Lawrencium", "Lead", "Lithium",
"Lutetium", "Magnesium", "Manganese", "Meitnerium", "Mendelevium",
"Mercury", "Molybdenum", "Neodymium", "Neon", "Neptunium",
"Nickel", "Niobium", "Nitrogen", "Nobelium", "Osmium",
"Oxygen", "Palladium", "Phosphorus", "Platinum", "Plutonium",
"Polonium", "Potassium", "Praseodymium", "Promethium", "Protactinium",
"Radium", "Radon", "Rhenium", "Rhodium", "Roentgenium", "Rubidium",
"Ruthenium", "Rutherfordium", "Samarium", "Scandium", "Seaborgium",
"Selenium", "Silicon", "Silver", "Sodium", "Strontium", "Sulfur",
"Tantalum", "Technetium", "Tellurium", "Terbium", "Thallium", "Thorium",
"Thulium", "Tin", "Titanium", "Tungsten", "Ununpentium", "Ununquadium",
"Ununtrium", "Uranium", "Vanadium", "Xenon", "Ytterbium", "Yttrium", "Zinc",
"Zirconium"};
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.grams2moles);
elementnumber = (TextView) findViewById(R.id.elementnumber);
grams = (EditText) findViewById(R.id.grams);
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {my_func01();}});
Spinner spin = (Spinner) findViewById(R.id.elements);
spin.setOnItemSelectedListener((OnItemSelectedListener) this);
ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, element);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
void my_func01(){
int grams2;
int elementnumber2;
int moles2;
grams2 = Integer.parseInt(grams.getText().toString());
elementnumber2 = Integer.parseInt(elementnumber.getText().toString());
moles2 = (grams2 * (1 / elementnumber2));
moles5.setText("" + moles2);
}
public void setSpinner01(Spinner spinner01) {
Spinner01 = spinner01;
}
public Spinner getSpinner01() {
return Spinner01;
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
if ("Actinium".equals(element[position]))
{elementnumber.setText("227");}
if ("Aluminum".equals(element[position]))
{elementnumber.setText("26.981538");}
if ("Americium".equals(element[position]))
{elementnumber.setText("243");}
if ("Antimony".equals(element[position]))
{elementnumber.setText("121.760");}
if ("Argon".equals(element[position]))
{elementnumber.setText("39.948");}
if ("Arsenic".equals(element[position]))
{elementnumber.setText("74.9216");}
if ("Astatine".equals(element[position]))
{elementnumber.setText("210");}
if ("Barium".equals(element[position]))
{elementnumber.setText("137.327");}
if ("Berkelium".equals(element[position]))
{elementnumber.setText("247");}
if ("Bismuth".equals(element[position]))
{elementnumber.setText("208.9804");}
if ("Bohrium".equals(element[position]))
{elementnumber.setText("264");}
if ("Boron".equals(element[position]))
{elementnumber.setText("10.811");}
if ("Bromine".equals(element[position]))
{elementnumber.setText("79.904");}
if ("Cadmium".equals(element[position]))
{elementnumber.setText("112.41");}
if ("Calcium".equals(element[position]))
{elementnumber.setText("40.078");}
if ("Californium".equals(element[position]))
{elementnumber.setText("251");}
if ("Carbon".equals(element[position]))
{elementnumber.setText("12.0107");}
if ("Cerium".equals(element[position]))
{elementnumber.setText("140.116");}
if ("Cesium".equals(element[position]))
{elementnumber.setText("132.90545");}
if ("Chlorine".equals(element[position]))
{elementnumber.setText("35.453");}
if ("Chromium".equals(element[position]))
{elementnumber.setText("51.996");}
if ("Copper".equals(element[position]))
{elementnumber.setText("63.546");}
if ("Curium".equals(element[position]))
{elementnumber.setText("247");}
}
public void onNothingSelected(AdapterView<?> arg0) {
elementnumber.setText("227");
}
}
我的错误是这个
09-22 20:02:58.353: ERROR/AndroidRuntime(17745): Uncaught handler: thread main exiting due to uncaught exception
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): java.lang.NullPointerException
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at table.periodic.Gram2mol.my_func01(Gram2mol.java:77)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at table.periodic.Gram2mol$1.onClick(Gram2mol.java:56)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.view.View.performClick(View.java:2364)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.view.View.onTouchEvent(View.java:4179)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.widget.TextView.onTouchEvent(TextView.java:6592)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.view.View.dispatchTouchEvent(View.java:3709)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.view.ViewRoot.handleMessage(ViewRoot.java:1697)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.os.Looper.loop(Looper.java:123)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at android.app.ActivityThread.main(ActivityThread.java:4568)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at java.lang.reflect.Method.invoke(Method.java:521)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): at dalvik.system.NativeStart.main(Native Method)
第77行是“moles5.setText(”“+ moles2);”
我不相信我会让任何身份混淆,所以我并不担心这一点,似乎不是那种错误。如果有人有任何想法让我知道。谢谢。
答案 0 :(得分:0)
moles5
是该行唯一可以为null的东西。您无法在基本类型上获得空指针。看起来好像你在任何地方初始化moles5
。
另外,打印数字的更好方法是说Integer.toString(moles2);
答案 1 :(得分:0)
您似乎没有实例化moles5
变量。如果您的layout/grams2moles.xml
文件中有一个ID为moles5
的TextView,您可以通过调用
onCreate()
中对其进行实例化
moles5 = (TextView) findViewById(R.id.moles5);