整数和计算

发布于 2024-09-24 17:29:49 字数 8788 浏览 2 评论 0原文

我正在使用整数并尝试进行涉及部分小数的计算。我以前使用过相同的设置并且它有效,但由于某种原因它不起作用。

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("" + molecular2);

我不相信我混淆了任何 id,所以我不担心这一点,而且它似乎不是那种错误。如果有人有任何想法请告诉我。谢谢。

I'm working with integers and trying to do calculations that involve decimals at parts. I have used this same setup before and it had worked but for some reason this isn't working.

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");
    }

}

my error is this

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)

line 77 is the "moles5.setText("" + moles2);"

I don't believe I'm getting any id's mixed up so I'm not worried about that and it doesn't seem to be that kind of error. If anyone has any ideas let me know. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫南 2024-10-01 17:29:49

您似乎没有实例化 moles5 变量。如果您的 layout/grams2moles.xml 文件中有一个 ID 为 moles5 的 TextView,您可以通过调用在 onCreate() 中实例化它

moles5 = (TextView) findViewById(R.id.moles5);

It seems that you are not instantiating the moles5 variable. If there is a TextView in your layout/grams2moles.xml file with an id of moles5, you would instantiate it in onCreate() by calling

moles5 = (TextView) findViewById(R.id.moles5);
以往的大感动 2024-10-01 17:29:49

moles5 是该行中唯一可以为 null 的内容。您无法在原始类型上获取空指针。但看起来您并没有在任何地方初始化 moles5

另外,打印数字的更好方法是 Integer.toString(moles2);

moles5 is the only thing on that line that can be null. You can't get a null pointer on a primitive type. It doesn't looks like you initialize moles5 anywhere though.

Also, a better way to print a number is to say Integer.toString(moles2);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文