Android EditText到int会话错误

时间:2016-04-15 23:21:32

标签: java android

这可能是最常见的问题,但很难找到答案。首先我是新手。我想制作简单的二次方程式公式app。这将允许快速找到解决方案。我碰到Android Studio说Code是okey的问题,但设备在打开应用程序后崩溃了。

private Button   mButton;
private EditText mEdit;
private EditText mEdit1;
private EditText mEdit2;
private TextView mText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mButton = (Button)findViewById(R.id.button2);
    mEdit   = (EditText)findViewById(R.id.editText);
    mEdit1   = (EditText)findViewById(R.id.editText2);
    mEdit2   = (EditText)findViewById(R.id.editText3);
    mText = (TextView)findViewById(R.id.textView4);

    //Int definēšana
    final int i1 = Integer.parseInt(mEdit.getText().toString());
    final int i2 = Integer.parseInt(mEdit1.getText().toString());
    final int i3 = Integer.parseInt(mEdit2.getText().toString());

    mButton.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    //Mainīgo ievade
                    int a = i1;
                    //Pārbauda vai a nav nulle
                    if (a == 0){
                        mText.setText(String.valueOf("Nav kvadrātvienādojums"));
                    } else {
                        int b = i2;
                        int c = i3;

                        //Diskriminanta aprēķināšana
                        double diskr = (b*b)-4*a*c;
                        //Kvadrātsakne no diskriminanta
                        double sd = (double) Math.sqrt(diskr);
                        //Sakņu aprēķināšana
                        double x1 = (-b+sd)/(2*a);
                        double x2 = (-b-sd)/(2*a);

                        //Rezultāta izvade
                        if (diskr < 0){
                            mText.setText(String.valueOf("Kvadrātvienādojumam nav sakņu"));
                        } else if (diskr == 0){
                            mText.setText(String.valueOf("Kvadrātvienādojumam ir viena sakne: " + x1));
                        } else {
                            mText.setText(String.valueOf("Kvadrātvienādojuma saknes: " + x1 + " un " + x2));
                        }
                    }
                }
            });
} 

可能有助于检测问题的logcat

FATAL EXCEPTION: main
Process: com.homemade.prtbust.kvadratvienadojums, PID: 4552
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.homem`enter code here`ade.prtbust.kvadratvienadojums/com.homemade.prtbust.kvadratvienadojums.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3133)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3243)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
    at com.homemade.prtbust.kvadratvienadojums.MainActivity.onCreate(MainActivity.java:38)
    at android.app.Activity.performCreate(Activity.java:6609)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3243)
    at android.app.ActivityThread.access$1000(ActivityThread.java:218)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6917)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

3 个答案:

答案 0 :(得分:1)

我想你错过了

findViewById&#34; activity_name是布局的名称,其中是编辑文本&#34;

在使用t

之前,必须先调用{p> r

答案 1 :(得分:0)

尝试编写此代码,为活动设置内容视图

    //load all the views from the xml file
    setContentView(R.layout.activity_main);

mButton = (Button)findViewById(R.id.button2);
mEdit   = (EditText)findViewById(R.id.editText);
mEdit1   = (EditText)findViewById(R.id.editText2);
mEdit2   = (EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.textView4);

//Int definēšana
final int i1 = Integer.parseInt(mEdit.getText().toString());
final int i2 = Integer.parseInt(mEdit1.getText().toString());
...

    mButton.setOnClickListener(/*your code here*/);

在创建EditText视图之前,您无法从EditText获取文本。 如果这没有解决你的问题,请尝试在onStart上编写你的i1 / 2/3变量

@Override
protected void onStart() {
    super.onStart();

    final int i1 = Integer.parseInt(mEdit.getText().toString());
    final int i2 = Integer.parseInt(mEdit1.getText().toString());
    final int i3 = Integer.parseInt(mEdit2.getText().toString());
}

答案 2 :(得分:0)

您没有为Activity分配任何布局,因此当您尝试通过代码访问其中任何一个时,您的所有EditTexts,Button和TextView都不会出现。

其次,你应该在单击按钮时读取变量的值,而不是在oncreate中,因为这会导致你的另一个错误,因为你会尝试解析一个空字符串的东西(除非你设置了一个xml中android:text标记中的值)。 我也不会将它们声明为最终版(因为您以后无法修改它们。

尝试使用此代码:

private Button   mButton;
private EditText mEdit;
private EditText mEdit1;
private EditText mEdit2;
private TextView mText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mButton = (Button)findViewById(R.id.button2);
    mEdit   = (EditText)findViewById(R.id.editText);
    mEdit1   = (EditText)findViewById(R.id.editText2);
    mEdit2   = (EditText)findViewById(R.id.editText3);
    mText = (TextView)findViewById(R.id.textView4);

    mButton.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    //Int definēšana
                    int i1 = Integer.parseInt(mEdit.getText().toString());
                    int i2 = Integer.parseInt(mEdit1.getText().toString());
                    int i3 = Integer.parseInt(mEdit2.getText().toString());

                    //Mainīgo ievade
                    int a = i1;
                    //Pārbauda vai a nav nulle
                    if (a == 0){
                        mText.setText(String.valueOf("Nav kvadrātvienādojums"));
                    } else {
                        int b = i2;
                        int c = i3;

                        //Diskriminanta aprēķināšana
                        double diskr = (b*b)-4*a*c;
                        //Kvadrātsakne no diskriminanta
                        double sd = (double) Math.sqrt(diskr);
                        //Sakņu aprēķināšana
                        double x1 = (-b+sd)/(2*a);
                        double x2 = (-b-sd)/(2*a);

                        //Rezultāta izvade
                        if (diskr < 0){
                            mText.setText(String.valueOf("Kvadrātvienādojumam nav sakņu"));
                        } else if (diskr == 0){
                            mText.setText(String.valueOf("Kvadrātvienādojumam ir viena sakne: " + x1));
                        } else {
                            mText.setText(String.valueOf("Kvadrātvienādojuma saknes: " + x1 + " un " + x2));
                        }
                    }
                }
            });
}