我的代码抛出错误[致命错误:主要]您能告诉我为什么吗?

时间:2019-02-09 07:28:30

标签: android

我在onCreate和setContentView之后创建所有对象,但仍然出现错误[致命错误:主要],这会使我的应用程序崩溃。我见过人们面临同样的问题,但是那是当他们在setContentView标记上方为EditText创建对象时,我尝试过移动它,但对我来说不起作用,希望您能为我提供帮助。我将为您提供代码...谢谢

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        //-----------------------------------------------------------------------------------------------------------------------
        //ASSIGNING
        final EditText name=(findViewById(R.id.editText));
        final EditText date=(findViewById(R.id.editText3));
        EditText month=(findViewById(R.id.editText4));
        final EditText year=(findViewById(R.id.editText5));
        Button b=(findViewById(R.id.button));

        //-----------------------------------------------------------------------------------------------------------------------
        //NAME NUMBER
        String s= name.getText().toString();
        char[] n=s.toCharArray();
        int i,total=0;

我遇到的错误:-

2019-02-09 13:24:08.085 6934-6934/com.example.numerologysolutions E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.numerologysolutions, PID: 6934
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.numerologysolutions/com.example.numerologysolutions.MainActivity}: java.lang.NumberFormatException: For input string: ""
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NumberFormatException: For input string: ""
    at java.lang.Integer.parseInt(Integer.java:627)
    at java.lang.Integer.parseInt(Integer.java:650)
    at com.example.numerologysolutions.MainActivity.onCreate(MainActivity.java:81)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

1 个答案:

答案 0 :(得分:1)

根据您的错误堆栈跟踪,这就是问题所在。您正在尝试将字符串转换为数字Caused by: java.lang.NumberFormatException: For input string: ""