-1

我想从ListView另一个活动中发送数据并将这些数据保存到SQLiteDatabase. 我使用ListView并将用户数据保存到SQLite并将此数据设置为ListView,我希望在单击列表视图项目时,将用户信息显示到另一个活动中。但告诉我这个错误:

java.lang.NumberFormatException: Invalid int: "null"

主页代码:

public class Example_mainPage extends AppCompatActivity {

    private ListView list_phoneBook;
    private dbOpenHelper_ex dbOpen_ex;
    private dbHandler_ex dbhandler_ex;
    private String[] nameArray;
    private int[] list_id;
    private Button insert_ex_btn, update_ex_btn, delete_ex_btn;

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

        dbhandler_ex = new dbHandler_ex(this);
        list_phoneBook = (ListView) findViewById(R.id.phoneBook_list);
        list_phoneBook.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent con_intent = new Intent(Example_mainPage.this, EX_Contacts_Page.class);
                con_intent.putExtra("id_show", list_id[position]);
                startActivity(con_intent);
            }
        });

        insert_ex_btn = (Button) findViewById(R.id.btn_insert_ex);
        insert_ex_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent insert_intent = new Intent(Example_mainPage.this, EX_Insert_Page.class);
                startActivity(insert_intent);
            }
        });
/*
        update_btn = (Button) findViewById(R.id.btn_update);
        update_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbhandler_ex.open();
                dbhandler_ex.update("Maryam", 1, 4);
                dbhandler_ex.close();
            }
        });

        delete_btn = (Button) findViewById(R.id.btn_delete);
        delete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbhandler_ex.open();
                dbhandler_ex.delete(2);
                dbhandler_ex.close();
            }
        });*/
    }

    @Override
    protected void onResume() {
        super.onResume();

        refresh();
    }

    public void refresh() {
        dbhandler_ex.open();
        int count = dbhandler_ex.count();
        nameArray = new String[count];
        list_id = new int[count];

        for (int i = 0; i < count; i++) {
            nameArray[i] = dbhandler_ex.display(i, 1) + "\n" + dbhandler_ex.display(i, 2);
            list_id[i] = Integer.parseInt(dbhandler_ex.display(i, 0));
        }
        dbhandler_ex.close();
        list_phoneBook.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nameArray));
    }

}

联系人页面代码:

public class EX_Contacts_Page extends AppCompatActivity {

    private dbHandler_ex dbHandlerEx;
    private TextView name_ex_contacts, family_ex_contacts, address_ex_contacts, email_ex_contacts, phone_ex_contacts;

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

        dbHandlerEx = new dbHandler_ex(this);

        Bundle extraBundle = getIntent().getExtras();
        String list_id = extraBundle.getString("id_show");

        name_ex_contacts = (TextView) findViewById(R.id.ex_contacts_name);
        family_ex_contacts = (TextView) findViewById(R.id.ex_contacts_family);
        address_ex_contacts = (TextView) findViewById(R.id.ex_contacts_address);
        email_ex_contacts = (TextView) findViewById(R.id.ex_contacts_email);
        phone_ex_contacts = (TextView) findViewById(R.id.ex_contacts_phone);

        dbHandlerEx.open();
        name_ex_contacts.setText(dbHandlerEx.display_id(Integer.parseInt(list_id), 1));
        family_ex_contacts.setText(dbHandlerEx.display_id(Integer.parseInt(list_id), 2));
        address_ex_contacts.setText(dbHandlerEx.display_id(Integer.parseInt(list_id), 3));
        email_ex_contacts.setText(dbHandlerEx.display_id(Integer.parseInt(list_id), 4));
        phone_ex_contacts.setText(dbHandlerEx.display_id(Integer.parseInt(list_id), 5));
        dbHandlerEx.close();

    }

    public void ex_update(View view){

    }

    public void ex_delete(View view){

    }
}

dbHandler 代码:

public class dbHandler_ex {

    private dbOpenHelper_ex dbOpen;
    private SQLiteDatabase database;

    public dbHandler_ex(Context context) {

        dbOpen = new dbOpenHelper_ex(context);
    }

    public void open() {
        database = dbOpen.getWritableDatabase();
    }

    public void close() {
        dbOpen.close();
    }

    public String display(int position, int field) {

        Cursor cursor = database.query(dbOpen.tblName, null, null, null, null, null, null);
        cursor.moveToPosition(position);
        String user_field = cursor.getString(field);

        return user_field;
    }

    public String display_id(int id, int field) {

        Cursor cursor = database.rawQuery("SELECT * FROM tbl_phonebook WHERE id="+id, null);
        cursor.moveToFirst();
        String user_field = cursor.getString(field);

        return user_field;
    }

    public void insert(String name, String family, String address, String email, String phone) {

        ContentValues contentValues = new ContentValues();
        contentValues.put(dbOpen.cName, name);
        contentValues.put(dbOpen.cFamily, family);
        contentValues.put(dbOpen.cAddress, address);
        contentValues.put(dbOpen.cEmail, email);
        contentValues.put(dbOpen.cPhone, phone);


        database.insert(dbOpen.tblName, dbOpen.cName, contentValues);

    }

    public Integer count() {

        Cursor cursor = database.query(dbOpen.tblName, null, null, null, null, null, null);
        int count_num = cursor.getCount();

        return count_num;
    }

    public void delete(int id) {
        database.delete(dbOpen.tblName, "id=" + id, null);
    }

    public void update(String value, int field, int id) {

        ContentValues contentValues = new ContentValues();

        database.update(dbOpen.tblName, contentValues, "id=" + id, null);
    }

}

LogCat 错误:

02-02 15:20:46.474 22609-22609/com.tellfa.nikandroid.database E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.tellfa.nikandroid.database, PID: 22609
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tellfa.nikandroid.database/com.tellfa.nikandroid.database.example.EX_Contacts_Page}: java.lang.NumberFormatException: Invalid int: "null"
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5349)
                                                                                    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:908)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                                                                                 Caused by: java.lang.NumberFormatException: Invalid int: "null"
                                                                                    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 com.tellfa.nikandroid.database.example.EX_Contacts_Page.onCreate(EX_Contacts_Page.java:32)
                                                                                    at android.app.Activity.performCreate(Activity.java:6020)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5349) 
                                                                                    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:908) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 

我怎样才能适应它?tnx 全部 <3

4

3 回答 3

0

您必须在 oncreate() 方法中调用 refresh() 方法,即

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

        dbhandler_ex = new dbHandler_ex(this);
        list_phoneBook = (ListView) findViewById(R.id.phoneBook_list);

        refresh();


        list_phoneBook.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent con_intent = new Intent(Example_mainPage.this, EX_Contacts_Page.class);
                con_intent.putExtra("id_show", list_id[position]);
                startActivity(con_intent);
            }
        });
于 2016-02-02T12:12:03.813 回答
0

你好对不起我懒得阅读所有代码现在分析它,但我能说的是logcat错误

java.lang.NumberFormatException:无效的int:“null”

您正在将 null 值解析数字null在java中不是有效数字,以另一种方式 null != 0。我希望它有所帮助

于 2016-02-02T12:13:23.037 回答
0

我认为您的问题在于将额外数据放入Intent. 你正在输入一个 int

con_intent.putExtra("id_show", list_id[position]);

并试图得到String

String list_id = extraBundle.getString("id_show");

将其更改为

int list_id = extraBundle.getInt("id_show");
于 2016-02-03T08:38:57.497 回答