NumberFormatException无效int""

时间:2015-06-11 13:34:11

标签: java android logcat numberformatexception

我正在尝试从EditText获取输入值,我想将其保存到sqlite数据库。我不知道如何使用logcat [还请解释如何从LogCat中读取错误]。

MainActivity.java:

package com.example.database;

import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity{

    EditText first,last,age,classc;
    Button add,view;

    String FirstName;
    String LastName,Class;
    Integer Age;

    sqLit myDB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDB = new sqLit(this);
        Link();
        xmlToVar();
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Integer flag=myDB.insertValue(FirstName, LastName, Class, Age);
                if (flag==1)
                {
                    Context context=getApplicationContext();
                    Toast toast = Toast.makeText(context, "Record Added" , Toast.LENGTH_LONG);
                    toast.show();
                }
                else
                {
                    Context context=getApplicationContext();
                    Toast toast = Toast.makeText(context, "Error Occured" , Toast.LENGTH_LONG);
                    toast.show();
                }
            }
        });
    }

    public void Link()
    {
        first=(EditText) findViewById(R.id.editFirst);
        last=(EditText) findViewById(R.id.editLast);
        classc=(EditText) findViewById(R.id.editClass);
        age=(EditText) findViewById(R.id.editAge);
        add=(Button) findViewById(R.id.Add);
        view=(Button) findViewById(R.id.ViewAll);
    }

    public void xmlToVar()
    {
        FirstName = first.getText().toString();
        LastName = last.getText().toString();
        Class = classc.getText().toString();
        Age = Integer.parseInt(age.getText().toString());
    }


}

sqLit.java:

package com.example.database;

import org.w3c.dom.Text;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class sqLit extends SQLiteOpenHelper
{

    public static final String DB_NAME="student12.db";
    public static final String TABLE_NAME="class1";

    public static final String COL_1="ROLL NO";
    public static final String COL_2="First Name";
    public static final String COL_3="Last Name";
    public static final String COL_4="Class";
    public static final String COL_5="Age";




    public sqLit(Context context) {
        super(context, DB_NAME, null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("Create table " + TABLE_NAME + "(" + COL_1 + "INTEGER AUTOINCREMENT PRIMARY KEY," + COL_2 + "TEXT," + COL_3 + "TEXT," + COL_4 + "TEXT," + COL_5 + "INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("Drop Table If Exist" + TABLE_NAME );
        onCreate(db);
    }

    public Integer insertValue(String FirstName, String LastName, String Class, Integer Age)
    {

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentV = new ContentValues();
        contentV.put(COL_2, FirstName);
        contentV.put(COL_3, LastName);
        contentV.put(COL_4, Class);
        contentV.put(COL_5, Age);
        long isInserted = db.insert(TABLE_NAME, null, contentV);
        if (isInserted == -1){
            return 0;
        }
        else
        {
            return 1;
        }
        }




}

另外,请解释如何使用LogCat,因为我不懂如何阅读它。

logcat的:

06-11 18:44:39.650: E/Trace(29536): error opening trace file: No such file or directory (2)
06-11 18:44:39.660: D/AndroidRuntime(29536): Shutting down VM
06-11 18:44:39.660: W/dalvikvm(29536): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:44:39.660: E/AndroidRuntime(29536): FATAL EXCEPTION: main
06-11 18:44:39.660: E/AndroidRuntime(29536): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.os.Looper.loop(Looper.java:137)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at dalvik.system.NativeStart.main(Native Method)
06-11 18:44:39.660: E/AndroidRuntime(29536): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:44:39.660: E/AndroidRuntime(29536):    at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at com.example.database.MainActivity.xmlToVar(MainActivity.java:72)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at com.example.database.MainActivity.onCreate(MainActivity.java:34)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:44:39.660: E/AndroidRuntime(29536):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:44:39.660: E/AndroidRuntime(29536):    ... 11 more
06-11 18:46:03.652: D/dalvikvm(30256): Not late-enabling CheckJNI (already on)
06-11 18:46:03.682: E/Trace(30256): error opening trace file: No such file or directory (2)
06-11 18:46:03.712: D/AndroidRuntime(30256): Shutting down VM
06-11 18:46:03.712: W/dalvikvm(30256): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:46:03.712: E/AndroidRuntime(30256): FATAL EXCEPTION: main
06-11 18:46:03.712: E/AndroidRuntime(30256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.os.Looper.loop(Looper.java:137)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at dalvik.system.NativeStart.main(Native Method)
06-11 18:46:03.712: E/AndroidRuntime(30256): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:46:03.712: E/AndroidRuntime(30256):    at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at com.example.database.MainActivity.xmlToVar(MainActivity.java:73)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at com.example.database.MainActivity.onCreate(MainActivity.java:35)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:46:03.712: E/AndroidRuntime(30256):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:46:03.712: E/AndroidRuntime(30256):    ... 11 more
06-11 18:53:33.378: E/Trace(2383): error opening trace file: No such file or directory (2)
06-11 18:53:33.408: D/AndroidRuntime(2383): Shutting down VM
06-11 18:53:33.408: W/dalvikvm(2383): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 18:53:33.408: E/AndroidRuntime(2383): FATAL EXCEPTION: main
06-11 18:53:33.408: E/AndroidRuntime(2383): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.os.Looper.loop(Looper.java:137)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at dalvik.system.NativeStart.main(Native Method)
06-11 18:53:33.408: E/AndroidRuntime(2383): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-11 18:53:33.408: E/AndroidRuntime(2383):     at java.lang.Integer.invalidInt(Integer.java:138)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at java.lang.Integer.parseInt(Integer.java:359)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at java.lang.Integer.parseInt(Integer.java:332)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at com.example.database.MainActivity.xmlToVar(MainActivity.java:73)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at com.example.database.MainActivity.onCreate(MainActivity.java:35)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.app.Activity.performCreate(Activity.java:5008)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-11 18:53:33.408: E/AndroidRuntime(2383):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-11 18:53:33.408: E/AndroidRuntime(2383):     ... 11 more

2 个答案:

答案 0 :(得分:3)

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

(在FATAL EXCEPTION:之后)告诉我们您正在尝试解析int,但您尝试解析的值为空String

这一行(引用你的类的第一行)告诉我们它是哪一行

 at com.example.database.MainActivity.xmlToVar(MainActivity.java:72)

这种情况正在发生,因为您在onCreate()输入任何值之前在EditText中调用它。

从上面一行中,我们可以看到第72行方法MainActivityxmlToVar发生的错误应该是

Age = Integer.parseInt(age.getText().toString());

要解决此问题,您应该在onClick或某个事件侦听器中调用该方法。您还应该将其包围在try/catch中或使用其他验证检查。

您应该阅读以下问题及其答案以获取更多详细信息:

What is a stack trace, and how can I use it to debug my application errors?

Unfortunately MyApp has stopped. How can I solve this?

java.lang.NumberFormatException: Invalid int: "" in android

基本上,阅读它给你的错误,然后寻找提到你的班级的第一行。有时您需要更深入地阅读,但通常已经足够了。

  

另外请解释如何使用LogCat,因为我是3天的Android程序员。我从未使用过Eclipse。

请注意,堆栈跟踪不是Eclipse,甚至不是Java / Android特定的。您可以在其他语言/ IDE /控制台

中获得某种类似的崩溃日志

答案 1 :(得分:0)

问题在于:

Age = Integer.parseInt(age.getText().toString());

age.getText().toString()等于"",导致NumberFormatException