小弟我这个空指针错误是在哪里出现的
我这个空指针异常是在哪里出现的?
public class DictionaryActivity extends Activity {
private EditText eng=null;
private EditText chi=null;
private Button insert=null;
private Button delete=null;
private Button query=null;
private String s1=null;
private String s2=null;
private String s3=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eng = (EditText) findViewById(R.id.eng);
chi = (EditText) findViewById(R.id.chi);
insert = (Button) findViewById(R.id.insert);
delete = (Button) findViewById(R.id.delete);
query = (Button) findViewById(R.id.query);
insert.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ContentValues values = new ContentValues();
values=null;
SQLite sq = new SQLite(DictionaryActivity.this, "dictionary");
SQLiteDatabase db = sq.getWritableDatabase();
if ((eng.getText()).toString() != "" && (chi.getText()).toString() != "") {
Log.d("进入","第一次判断");
s1 = eng.getText().toString();
s2 = chi.getText().toString();
System.out.println(s1+s2+".");
if (Pattern.compile("\\w+").matcher(s1).matches()) {
values.put("word", s1);
values.put("explain", s2);
db.insert("dictionary", null, values);
System.out.println("插入数据成功");
} else {
Toast.makeText(DictionaryActivity.this, "请输入正确的单词",
Toast.LENGTH_SHORT).show();
}
}
}
});
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SQLite sq = new SQLite(DictionaryActivity.this, "dictionary");
SQLiteDatabase db = sq.getWritableDatabase();
}
});
query.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SQLite sq = new SQLite(DictionaryActivity.this, "dictionary");
SQLiteDatabase db = sq.getReadableDatabase();
}
});
}
}
还有getText() 返回的是editTable
------解决方案--------------------
应该是你设置了values = null;当values.put("word",s1)时就空指针异常了
public class DictionaryActivity extends Activity {
private EditText eng=null;
private EditText chi=null;
private Button insert=null;
private Button delete=null;
private Button query=null;
private String s1=null;
private String s2=null;
private String s3=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eng = (EditText) findViewById(R.id.eng);
chi = (EditText) findViewById(R.id.chi);
insert = (Button) findViewById(R.id.insert);
delete = (Button) findViewById(R.id.delete);
query = (Button) findViewById(R.id.query);
insert.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ContentValues values = new ContentValues();
values=null;
SQLite sq = new SQLite(DictionaryActivity.this, "dictionary");
SQLiteDatabase db = sq.getWritableDatabase();
if ((eng.getText()).toString() != "" && (chi.getText()).toString() != "") {
Log.d("进入","第一次判断");
s1 = eng.getText().toString();
s2 = chi.getText().toString();
System.out.println(s1+s2+".");
if (Pattern.compile("\\w+").matcher(s1).matches()) {
values.put("word", s1);
values.put("explain", s2);
db.insert("dictionary", null, values);
System.out.println("插入数据成功");
} else {
Toast.makeText(DictionaryActivity.this, "请输入正确的单词",
Toast.LENGTH_SHORT).show();
}
}
}
});
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SQLite sq = new SQLite(DictionaryActivity.this, "dictionary");
SQLiteDatabase db = sq.getWritableDatabase();
}
});
query.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SQLite sq = new SQLite(DictionaryActivity.this, "dictionary");
SQLiteDatabase db = sq.getReadableDatabase();
}
});
}
}
还有getText() 返回的是editTable
------解决方案--------------------
应该是你设置了values = null;当values.put("word",s1)时就空指针异常了