如何解决错误:不幸<my app =“”>已停止

时间:2017-01-12 06:42:27

标签: java android

我对Android编程完全陌生。我正在制作有史以来第一个应用程序的一些问题。每次我运行应用程序时,都会弹出一条消息,说“不幸的是,MyNewApp3已经停止”在我的手机上(我用来测试和调试我的应用程序)。此错误甚至不会让我的应用程序打开。我该如何解决这个问题?

这是我的代码:

package newapp.com.bipl.mynewapp3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Page2 extends AppCompatActivity {

RadioGroup Rg;
RadioButton B;
RadioButton O;
RadioButton H;
EditText Dec;
TextView Ans;
int Num;
String Result;
int b;
int c;
char z;
String s;
String d;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page2);

    Toast.makeText(getBaseContext(),"Successful Login",Toast.LENGTH_LONG).show();

    Rg = (RadioGroup) findViewById(R.id.RGroup);
    B = (RadioButton) findViewById(R.id.Binary_rb);
    O = (RadioButton) findViewById(R.id.Octal_rb);
    H = (RadioButton) findViewById(R.id.Hexa_rb);
    Dec = (EditText) findViewById(R.id.NumEntry_txt);
    Ans = (TextView) findViewById(R.id.Answer_lbl);
    Num = Integer.parseInt(Dec.getText().toString());
    //Num = 161;
    if(B.isSelected())
        Binary(Num);
    if(O.isSelected())
        Octal(Num);
    if (H.isSelected())
        Hexal(Num);

}

/* public void Conversion(){
    if(B.isSelected())
        Binary(Num);
    if(O.isSelected())
        Octal(Num);
    if (H.isSelected())
        Hexal(Num);
}*/

public void Binary(int a){

    while(b > 0){
        c = a % 2;
        b = a/2;
        d = "" + c;
        s = d.concat(s);
        a = b;
    }
    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);

}

public void Octal(int a){

    while(b > 0){
        c = a % 8;
        b = a/8;
        d = "" + c;
        s = d.concat(s);
        a = b;
    }
    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);

}

public void Hexal(int a){

    while(b > 0){
        c = a % 16;
        b = a/16;

        if(c>9){
            switch (c){
                case 10 : z = 'A';
                          break;
                case 11 : z = 'B';
                          break;
                case 12 : z = 'C';
                          break;
                case 13 : z = 'D';
                          break;
                case 14 : z = 'E';
                          break;
                case 15 : z = 'F';
                          break;
            }

            d = "" + z;

        }
        else
            d = "" + c;

        s = d.concat(s);
        a = b;
    }

    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);


  }
}

我的LogCat输出(我看过人们建议其他人放置Logcat输出后我发布了;我不知道它用于什么):

  

01-12 11:45:52.559 15441-15441 / newapp.com.bipl.mynewapp3 V / TextView:   stopSelectionActionMode()       01-12 11:45:52.572 15441-15441 / newapp.com.bipl.mynewapp3 D / AndroidRuntime:关闭VM       01-12 11:45:52.573 15441-15441 / newapp.com.bipl.mynewapp3 E / AndroidRuntime:FATAL EXCEPTION:main       流程:newapp.com.bipl.mynewapp3,PID:15441       java.lang.RuntimeException:无法启动活动ComponentInfo {newapp.com.bipl.mynewapp3 / newapp.com.bipl.mynewapp3.Page2}:   java.lang.NumberFormatException:无效的int:“”       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)       在android.app.ActivityThread.access $ 800(ActivityThread.java:178)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1470)       在android.os.Handler.dispatchMessage(Handler.java:111)       在android.os.Looper.loop(Looper.java:194)       在android.app.ActivityThread.main(ActivityThread.java:5631)       at java.lang.reflect.Method.invoke(Native Method)       在java.lang.reflect.Method.invoke(Method.java:372)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:959)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)       引起:java.lang.NumberFormatException:无效的int:“”       在java.lang.Integer.invalidInt(Integer.java:138)       在java.lang.Integer.parseInt(Integer.java:358)       在java.lang.Integer.parseInt(Integer.java:334)       at newapp.com.bipl.mynewapp3.Page2.onCreate(Page2.java:41)       在android.app.Activity.performCreate(Activity.java:6092)       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)       在android.app.ActivityThread.access $ 800(ActivityThread.java:178)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1470)       在android.os.Handler.dispatchMessage(Handler.java:111)       在android.os.Looper.loop(Looper.java:194)       在android.app.ActivityThread.main(ActivityThread.java:5631)       at java.lang.reflect.Method.invoke(Native Method)       在java.lang.reflect.Method.invoke(Method.java:372)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:959)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)       01-12 11:50:52.616 15441-15441 / newapp.com.bipl.mynewapp3 I /处理:发送信号。 PID:15441 SIG:9

7 个答案:

答案 0 :(得分:0)

每次显示或使用任何变量进行转换时都必须检查null,问题是你没有检查字符串为null,在你的情况下它是null。 所以执行

if(Dec != null){
    if(Dec.getText() != null){
        Num = Integer.parseInt(Dec.getText().toString());
    }
}

它应该可以正常工作!

答案 1 :(得分:0)

声明并为您的&#39; Num&#34;变量如下

int Num = 0;

然后首先以字符串格式获取EditText值,然后解析那个

String mynum = Dec.getText().toString();
Num = Integer.parseInt(mynum);

您可以在这里检查myNum是否为空。

答案 2 :(得分:0)

您可以添加try-catch将String转换为int。如果它变得像空字符串那样无法转换,就不会崩溃。

try
{
    if(str != null){
      Num = Integer.parseInt(Dec.getText().toString());
    }
    else{
      Num = 0;
    }
}
catch (NumberFormatException e)
{
    Num = 0;
}

此实现将因任何错误的输入转换而停止崩溃。

答案 3 :(得分:0)

当onCreate()方法中的EditText init时,其中没有输入任何内容,因此Dec.getText().toString"",当然,您无法调用Integer.parseInt("")以便你得到了NumberFormatException

因此,您应该在parseInt()

之前添加检查
if (!"".equal(Dec.getText().toString)){
    Num = Integer.parseInt(Dec.getText().toString());
}

答案 4 :(得分:0)

问题是,一旦应用程序启动,您尝试使用解析Int将空字符串转换为整数。有关详细信息,请参阅this stackoverflow post。因此,您只需检查editText是否返回空字符串,或者您也可以尝试捕获异常。

此外,logcat非常有用,它可以帮助您识别导致问题的部分或代码行。检查logcat -

Caused by: java.lang.NumberFormatException: Invalid int: ""
                                                                           at java.lang.Integer.invalidInt(Integer.java:138)
                                                                           at java.lang.Integer.parseInt(Integer.java:358)
                                                                           at java.lang.Integer.parseInt(Integer.java:334)
                                                                           at newapp.com.bipl.mynewapp3.Page2.onCreate(Page2.java:41)

它清楚地告诉你,parseInt方法抛出异常并点击下面的41行甚至会带你到那段代码。

  

newapp.com.bipl.mynewapp3.Page2.onCreate(Page2.java:41)

试试这段代码 -

package newapp.com.bipl.mynewapp3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Page2 extends AppCompatActivity {

RadioGroup Rg;
RadioButton B;
RadioButton O;
RadioButton H;
EditText Dec;
TextView Ans;
int Num;
String Result;
int b;
int c;
char z;
String s;
String d;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page2);

    Toast.makeText(getBaseContext(),"Successful Login",Toast.LENGTH_LONG).show();

    Rg = (RadioGroup) findViewById(R.id.RGroup);
    B = (RadioButton) findViewById(R.id.Binary_rb);
    O = (RadioButton) findViewById(R.id.Octal_rb);
    H = (RadioButton) findViewById(R.id.Hexa_rb);
    Dec = (EditText) findViewById(R.id.NumEntry_txt);
    Ans = (TextView) findViewById(R.id.Answer_lbl);

    //Num = 161;

if ("".equals(Dec.getText().toString()))
 {   if(B.isSelected() || O.isSelected() || H.isSelected())
       Ans.setText("Empty field");     }
else{if(B.isSelected())
            Binary(Num);
        if(O.isSelected())
            Octal(Num);
        if (H.isSelected())
            Hexal(Num);}


    }

/* public void Conversion(){
    if(B.isSelected())
        Binary(Num);
    if(O.isSelected())
        Octal(Num);
    if (H.isSelected())
        Hexal(Num);
}*/

public void Binary(int a){
    Num = Integer.parseInt(Dec.getText().toString());    
    while(b > 0){
        c = a % 2;
        b = a/2;
        d = "" + c;
        s = d.concat(s);
        a = b;
    }
    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);

}

public void Octal(int a){
    Num = Integer.parseInt(Dec.getText().toString());
    while(b > 0){
        c = a % 8;
        b = a/8;
        d = "" + c;
        s = d.concat(s);
        a = b;
    }
    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);

}

public void Hexal(int a){
    Num = Integer.parseInt(Dec.getText().toString());
    while(b > 0){
        c = a % 16;
        b = a/16;

        if(c>9){
            switch (c){
                case 10 : z = 'A';
                          break;
                case 11 : z = 'B';
                          break;
                case 12 : z = 'C';
                          break;
                case 13 : z = 'D';
                          break;
                case 14 : z = 'E';
                          break;
                case 15 : z = 'F';
                          break;
            }

            d = "" + z;

        }
        else
            d = "" + c;

        s = d.concat(s);
        a = b;
    }

    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);


  }
}

答案 5 :(得分:0)

检查Null例外:

if (TextUtils.isEmpty(Dec.getText().toString())){
        //value is empty
}else {
       Num = Integer.parseInt(Dec.getText().toString());
}
if(B.isSelected())
    Binary(Num);
if(O.isSelected())
    Octal(Num);
if (H.isSelected())
    Hexal(Num);

           ......
          .............

答案 6 :(得分:0)

您必须创建一些按钮或某些动作侦听器,例如edittext ontextchangeListener,然后您必须按如下所示放置代码:

如果您使用按钮:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!Dec.getText.toString.equals(""))
              Num = Integer.parseInt(Dec.getText().toString());
        }
    });

如果您使用的是edittext textchangelistener:

TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        if(s.length() > 0)
           Num = Integer.parseInt(s.getText().toString());
    }
};

代码的问题是edittext在解析它时没有任何值。所以要解析它,ParseInt需要一些价值。所以更好的方法是采取一些按钮动作。