在我的项目中,我正在与数据库进行交互,按下时我有一个按钮从三个不同的编辑文本中添加三个项目,例如名称(字符串)、idcardno(最大 4 位的整数)、电话号码(最大 10 位的整数)。在按下添加按钮时,它会产生如下异常..
09-19 01:28:31.491: E/AndroidRuntime(1160): FATAL EXCEPTION: main
09-19 01:28:31.491: E/AndroidRuntime(1160): java.lang.IllegalStateException: Could not execute method of the activity
09-19 01:28:31.491: E/AndroidRuntime(1160): at android.view.View$1.onClick(View.java:3633)
09-19 01:28:31.491: E/AndroidRuntime(1160): at android.view.View.performClick(View.java:4240)
09-19 01:28:31.491: E/AndroidRuntime(1160): at android.view.View$PerformClick.run(View.java:17721)
09-19 01:28:31.491: E/AndroidRuntime(1160): at android.os.Handler.handleCallback(Handler.java:730)
09-19 01:28:31.491: E/AndroidRuntime(1160): at android.os.Handler.dispatchMessage(Handler.java:92)
09-19 01:28:31.491: E/AndroidRuntime(1160): at android.os.Looper.loop(Looper.java:137)
09-19 01:28:31.491: E/AndroidRuntime(1160): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-19 01:28:31.491: E/AndroidRuntime(1160): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 01:28:31.491: E/AndroidRuntime(1160): at java.lang.reflect.Method.invoke(Method.java:525)
09-19 01:28:31.491: E/AndroidRuntime(1160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-19 01:28:31.491: E/AndroidRuntime(1160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-19 01:28:31.491: E/AndroidRuntime(1160): at dalvik.system.NativeStart.main(Native Method)
09-19 01:28:31.491: E/AndroidRuntime(1160): Caused by: java.lang.reflect.InvocationTargetException
09-19 01:28:31.491: E/AndroidRuntime(1160): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 01:28:31.491: E/AndroidRuntime(1160): at java.lang.reflect.Method.invoke(Method.java:525)
09-19 01:28:31.491: E/AndroidRuntime(1160): at android.view.View$1.onClick(View.java:3628)
09-19 01:28:31.491: E/AndroidRuntime(1160): ... 11 more
09-19 01:28:31.491: E/AndroidRuntime(1160): Caused by: java.lang.NumberFormatException: Invalid int: "4233494398"
09-19 01:28:31.491: E/AndroidRuntime(1160): at java.lang.Integer.invalidInt(Integer.java:138)
09-19 01:28:31.491: E/AndroidRuntime(1160): at java.lang.Integer.parse(Integer.java:378)
09-19 01:28:31.491: E/AndroidRuntime(1160): at java.lang.Integer.parseInt(Integer.java:366)
09-19 01:28:31.491: E/AndroidRuntime(1160): at java.lang.Integer.parseInt(Integer.java:332)
09-19 01:28:31.491: E/AndroidRuntime(1160): at com.wordpress.smdaudhilbe.database.MainActivity.addData(MainActivity.java:59)
09-19 01:28:31.491: E/AndroidRuntime(1160): ... 14 more
09-19 01:28:34.340: I/Process(1160): Sending signal. PID: 1160 SIG: 9
这是我的完整代码,
package com.wordpress.smdaudhilbe.database;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
public EditText nameField,idCardField,phoneField;
public Button add,del,update;
public Spinner spN;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
spN.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
nameField.setText(parent.getItemAtPosition(position - 1).toString());
idCardField.setText(parent.getItemAtPosition(position).toString());
phoneField.setText(parent.getItemAtPosition(position+1).toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void addData(View v) {
initViews();
DataBaseHelper dbH = new DataBaseHelper(getApplicationContext());
dbH.insertContact(nameField.getText().toString(),Integer.parseInt(idCardField.getText().toString()),Integer.parseInt(phoneField.getText().toString()));
loadSpinner();
return;
}
private void loadSpinner() {
initViews();
DataBaseHelper dbH = new DataBaseHelper(getApplicationContext());
List<String> label = dbH.getIds();
ArrayAdapter<String> adpt = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,label);
spN.setAdapter(adpt);
}
public void deleteData(View v) {
initViews();
DataBaseHelper dbH = new DataBaseHelper(getApplicationContext());
dbH.deleteContact(Integer.parseInt(idCardField.getText().toString()));
}
private void initViews(){
nameField = (EditText)findViewById(R.id.editText1name);
idCardField = (EditText)findViewById(R.id.editText2idcard);
phoneField = (EditText)findViewById(R.id.editText3phone);
add = (Button)findViewById(R.id.button1);
del = (Button)findViewById(R.id.button2);
update = (Button)findViewById(R.id.button3);
spN = (Spinner)findViewById(R.id.spinner1);
}
}
class DataBaseHelper extends SQLiteOpenHelper{
public DataBaseHelper(Context context) {
super(context,"MyDataBase", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table contacts(name text not null,idCard integer,phoneNumber integer(10));");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists contacts;");
}
public void insertContact(String name,int idcard,int phone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues vals = new ContentValues();
vals.put("name",name);
vals.put("idCard", idcard);
vals.put("phoneNumber", phone);
db.insert("contacts", null, vals);
db.close();
}
public void deleteContact(long idcard) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete("contacts", "idCard=?",new String[] {String.valueOf(idcard)});
db.close();
}
public List<String> getIds() {
SQLiteDatabase db = this.getReadableDatabase();
List<String> lst = new ArrayList<String>();
Cursor cursor = db.rawQuery("select * from contacts;",null);
if(cursor.moveToFirst()){
do{
lst.add(cursor.getString(2));
}while(cursor.moveToNext());
}
cursor.close();
db.close();
return lst;
}
}
如何解决这个...?请帮助,我读到很多都指向此类问题的空指针异常。
供您参考,我的 main_activity.xml 是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2idcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:digits="0123456789"
android:maxLength="4" />
<EditText
android:id="@+id/editText3phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:digits="0123456789"
android:maxLength="10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:onClick="addData" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="del"
android:onClick="deleteData"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="update"
android:onClick="updateData"/>
</LinearLayout>
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
更新:当我尝试使用时
try{
dbH.insertContact(nameField.getText().toString(),Integer.parseInt(idCardField.getText().toString()),Integer.parseInt(phoneField.getText().toString()));
}catch (Exception e){e.printStackTrace();}
addData() 方法,
我懂了,
09-19 02:33:39.485: E/AndroidRuntime(933): FATAL EXCEPTION: main
09-19 02:33:39.485: E/AndroidRuntime(933): java.lang.NullPointerException
09-19 02:33:39.485: E/AndroidRuntime(933): at com.wordpress.smdaudhilbe.database.MainActivity$1.onItemSelected(MainActivity.java:41)
09-19 02:33:39.485: E/AndroidRuntime(933): at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
09-19 02:33:39.485: E/AndroidRuntime(933): at android.widget.AdapterView.access$200(AdapterView.java:49)
09-19 02:33:39.485: E/AndroidRuntime(933): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
09-19 02:33:39.485: E/AndroidRuntime(933): at android.os.Handler.handleCallback(Handler.java:730)
09-19 02:33:39.485: E/AndroidRuntime(933): at android.os.Handler.dispatchMessage(Handler.java:92)
09-19 02:33:39.485: E/AndroidRuntime(933): at android.os.Looper.loop(Looper.java:137)
09-19 02:33:39.485: E/AndroidRuntime(933): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-19 02:33:39.485: E/AndroidRuntime(933): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 02:33:39.485: E/AndroidRuntime(933): at java.lang.reflect.Method.invoke(Method.java:525)
09-19 02:33:39.485: E/AndroidRuntime(933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-19 02:33:39.485: E/AndroidRuntime(933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-19 02:33:39.485: E/AndroidRuntime(933): at dalvik.system.NativeStart.main(Native Method)
09-19 02:33:42.555: I/Process(933): Sending signal. PID: 933 SIG: 9
我不认为,我犯了任何错误。使用 initViews() 方法,
private void initViews(){
nameField = (EditText)findViewById(R.id.editText1name);
idCardField = (EditText)findViewById(R.id.editText2idcard);
phoneField = (EditText)findViewById(R.id.editText3phone);
add = (Button)findViewById(R.id.button1);
del = (Button)findViewById(R.id.button2);
update = (Button)findViewById(R.id.button3);
spN = (Spinner)findViewById(R.id.spinner1);
}
我已经初始化了所有字段
我还公开了每个领域。
public EditText nameField,idCardField,phoneField;
public Button add,del,update;
public Spinner spN;
请帮助追查原因。