简体   繁体   English

E / AndroidRuntime:致命异常:主要

[英]E/AndroidRuntime﹕ FATAL EXCEPTION: main

Whenever I try to run my application, an error is showing up in the LogCat. 每当我尝试运行我的应用程序时,LogCat都会显示错误。 This is my code in MainActivity.java 这是我在MainActivity.java中的代码

package com.practice.bludworth.practiceapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;


public class MainActivity extends Activity {

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

    EditText ageInput = (EditText) findViewById(R.id.ageReceived);
    int input = Integer.parseInt(ageInput.getText().toString());

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
   }
}

The error in LogCat says it's: LogCat中的错误说是:

Caused by: java.lang.NumberFormatException: Invalid int: ""

Very confused as I'm new to programming in general. 因为我是编程新手,所以非常困惑。 Thanks 谢谢

The problem is that you are attempting to parse an Integer on the start of the application, since onCreate is the first method that runs your EditText field has no value. 问题是您正在尝试在应用程序启动时解析Integer,因为onCreate是运行EditText字段的第一个方法,没有任何价值。

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyActivity extends Activity implements View.OnClickListener
{
    private EditText ageInput;

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

        // A button on your xml layout
        Button button = (Button) findViewById(R.id.button);

        // set the on click listener to this class, notice that MyActivity implements View.OnClickListener
        button.setOnClickListener(this);

        // This retrieves the EditText control
        ageInput = (EditText) findViewById(R.id.ageReceived);
    }

    // This method is called when your button is clicked.

    @Override
    public void onClick(View v)
    {


         // Switch cases are equivalent to if statements
         switch (v.getId())
         {
             // if your button was clicked.
             case R.id.button:
                // get the input
                 int input = Integer.parseInt(ageInput.getText().toString());

                 // Print the input to the console
                 Log.d("DEBUG_TAG", String.valueOf(input));
                 break;

         }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM