简体   繁体   English

如何解决错误:很遗憾 <my app> 已经停止

[英]How to solve the error : Unfortunately <my app> has stopped

I am completely new to Android programming. 我是Android编程的新手。 I am having some issues with the first ever app I am making. 我正在制作的第一个应用程序遇到一些问题。 Every time I run the app, a message pops up saying "Unfortunately, MyNewApp3 has stopped" on my mobile phone (which I am using to test and debug my app). 每次我运行该应用程序时,都会在我的手机(用于测试和调试应用程序)上弹出一条消息,提示“不幸的是,MyNewApp3已停止”。 This error doesn't even let my app open. 此错误甚至都无法打开我的应用。 How do I solve this problem? 我该如何解决这个问题?

Here is my code: 这是我的代码:

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


  }
}

My LogCat output (this I have posted after having seen people advise others to put the Logcat output; I don't know what it is used for) : 我的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: Shutting down VM 01-12 11:45:52.573 15441-15441/newapp.com.bipl.mynewapp3 E/AndroidRuntime: FATAL EXCEPTION: main Process: newapp.com.bipl.mynewapp3, PID: 15441 java.lang.RuntimeException: Unable to start activity ComponentInfo{newapp.com.bipl.mynewapp3/newapp.com.bipl.mynewapp3.Page2}: java.lang.NumberFormatException: Invalid int: "" at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595) at android.app.ActivityThread.access$800(ActivityThread.java:178) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5631) at java.lang.reflect.Method.invoke(Nativ 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:Shutting VM 01-12 11:45:52.573 15441-15441 / newapp.com.bipl.mynewapp3 E / AndroidRuntime:致命例外:主进程: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 $ H.handleMessage(ActivityThread.java:1470)上的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)在java.lang.reflect.Method .invoke(Nativ e Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 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) at android.app.Activity.performCreate(Activity.java:6092) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595) at android.app.ActivityThread.access$800(ActivityThread.java:178) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.jav e方法),位于com.android.internal.os处com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:959)处的java.lang.reflect.Method.invoke(Method.java:372)。 ZygoteInit.main(ZygoteInit.java:754)原因:java.lang.NumberFormatException:无效的int:“”,位于java.lang.Integer.invalidInt(Integer.java:138),位于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)at 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 android.os.Handler.dispatchMessage(Handler.java:111)上的.app.ActivityThread.access $ 800(ActivityThread.java:178)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1470)在android.os .Looper.loop(Looper.jav a:194) at android.app.ActivityThread.main(ActivityThread.java:5631) 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:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 01-12 11:50:52.616 15441-15441/newapp.com.bipl.mynewapp3 I/Process: Sending signal. a:194),位于android.app.ActivityThread.main(ActivityThread.java:5631),位于java.lang.reflect.Method.invoke(本机方法),位于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 / Process:正在发送信号。 PID: 15441 SIG: 9 PID:15441 SIG:9

You have to check for null every time you display or use any variable to convert here the issue is you are not checking the string to be null, and in your case it is null. 每次显示或使用任何变量进行转换时,您都必须检查null,这是因为您没有检查字符串是否为null,在您的情况下为null。 so perform 所以执行

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

It should work fine! 它应该工作正常!

Declare and assign zero to your 'Num" Variable as follows 声明并为“ Num”变量分配零,如下所示

int Num = 0;

Then first Get your EditText Value in String Format and then parse That 然后首先以字符串格式获取您的EditText值,然后解析

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

Here you can Check your myNum is null or not. 在这里,您可以检查myNum是否为null。

You can add try-catch to convert String to int. 您可以添加try-catch将String转换为int。 Won't crash if it gets anything like empty string which can't possible to convert. 如果它得到无法转换的空字符串之类的信息,则不会崩溃。

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

This implementation will stop crashing for any wrong input conversion. 该实现将停止因任何错误的输入转换而崩溃。

when the EditText init in onCreate() method,there are nothing typed in it,so Dec.getText().toString is "" ,of course, you can't invoke Integer.parseInt("") so that you got the NumberFormatException . 当在onCreate()方法中初始化EditText时,没有输入任何内容,因此Dec.getText().toString"" ,当然,您无法调用Integer.parseInt("")从而得到NumberFormatException

As a result,you should add a checking before parseInt() 因此,您应该在parseInt()之前添加检查

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

The problem is that you try to convert an empty string to integer using parse Int as soon as the app starts. 问题是,您尝试在应用启动后立即使用parse Int将空字符串转换为整数。 See this stackoverflow post for more info. 有关更多信息,请参见此stackoverflow帖子 So you just check if the editText is returning a null string or you can also try to catch the exception. 因此,您只需检查editText是否返回空字符串,或者也可以尝试捕获异常。

Also logcat is very useful, it helps you to identify which part or line of code is causing the problem. logcat也是非常有用的,它可以帮助您确定导致问题的代码的哪一部分。 Check the 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)

It clearly tells you that parseInt method is throwing an exception and clicking on 41 in below line even takes you to that piece of code. 它清楚地告诉您parseInt方法引发异常,然后单击下面一行中的41,甚至将您带到那段代码。

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

Try this code- 试试这个代码-

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


  }
}

Check for Null exception: 检查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);

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

You have to create some button or some action listener like edittext ontextchangeListener and then you have to put your code as below: 您必须创建一些按钮或某些动作侦听器(例如edittext ontextchangeListener),然后必须将代码如下所示:

If you are using button: 如果您使用按钮:

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

If you are using edittext textchangelistener: 如果您正在使用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());
    }
};

The problem with your code is edittext doesn't have any value when you are parsing it. 您的代码的问题是,在解析时,edittext没有任何值。 So to parse it, ParseInt needs some value. 因此要解析它,ParseInt需要一些值。 So better approach will be to take it on some button action. 因此,更好的方法是采取一些按钮操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不幸 <app> 已在android模拟器错误中停止。 如何解决呢? - Unfortunately <app> has stopped in android emulator error. How to solve this? Android studio错误:不幸的是“ App”已停止。 如何解决呢? - Android studio error: Unfortunately “App” has Stopped. How to solve this? 不幸, <your app> 已停止,而我正在尝试在我的android模拟器中运行它。 如何解决呢? - unfortunately, <your app> has stopped while i am trying it run it in my android emulator. how to solve this? 不幸的是,应用程序已停止,我该如何解决? - unfortunately app has stopped,how can i solve it? “不幸的是,应用程序已停止”错误? - “Unfortunately app has stopped” error? 不幸 <app><package> 已在android emulator-error中停止,该如何解决? - unfortunately <app><package> has stopped in android emulator-error ,How to solve this? 收到此错误:运行我的应用程序时,不幸的是该应用程序已停止 - Receive this error: unfortunately the app has stopped, when running my app 不幸的是,我的Android应用错误已停止 - my android app error unfortunately app has stopped Android Studio错误:很遗憾,我自己的应用已停止 - Android Studio error: Unfortunately My Own App has stopped 错误:不幸的是我的拳头应用程序已停止工作 - Error : Unfortunately My Fist App has stopped working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM