我是android开发的新手,我试过联系crud函数,它在onItemClick中抛出NullPointer异常错误,请帮帮我.. 这是我试过的代码, 工作流, 1.添加新联系人将数据保存在数据库中,仅列出主活动中的FirstName 2.在列表中选择FirstName应转到编辑活动,并且必须填写resp。来自DB的editText
enter code here
**Main Activity**
public class ContactActivity extends ListActivity {
Intent objIntent;
TextView contactId;
DBContoller controller = new DBContoller(this);
public void showAddForm(View view) {
Intent showIntent = new Intent(getApplicationContext(), AddContact.class);
startActivity(showIntent);
}
// public void closeShowAddForm(View view) {
// Intent closeIntent = new Intent(getApplicationContext(), )
//}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> contactList = controller.getAllContact();
if(contactList.size()!=0) {
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
//@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
contactId = (TextView) view.findViewById(R.id.CId);
String valContactId =contactId.getText().toString();
Intent objIndent = new Intent(ContactActivity.this,EditContact.class);
objIndent.putExtra("str_CId", valContactId);
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( ContactActivity.this, contactList, R.layout.view, new String[] {"CId","FirstName"}, new int[] {R.id.CId, R.id.FirstName,});
super.setListAdapter(adapter);
}
}
}
**Add Contact Activity**
public class AddContact extends Activity{
Button btnadd;
EditText fname, lname, mail, phn,addr, web, blog;
DBContoller controller = new DBContoller(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
fname = (EditText) findViewById(R.id.editText1);
lname = (EditText) findViewById(R.id.editText2);
mail = (EditText) findViewById(R.id.editText3);
phn = (EditText) findViewById(R.id.editText4);
addr = (EditText) findViewById(R.id.editText5);
web = (EditText) findViewById(R.id.editText6);
blog = (EditText) findViewById(R.id.editText7);
btnadd = (Button) findViewById(R.id.btnadd);
btnadd.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getApplicationContext(), ContactActivity.class);
startActivity(objIntent);
String savefn = fname.getText().toString();
String saveln = lname.getText().toString();
String savemail = mail.getText().toString();
String savemob = phn.getText().toString();
String saveaddr = addr.getText().toString();
String saveweb = web.getText().toString();
String saveblog = blog.getText().toString();
HashMap<String, String> queryValues = new HashMap<String, String>();
queryValues.put("FirstName", fname.getText().toString());
queryValues.put("LastName", lname.getText().toString());
queryValues.put("Email", mail.getText().toString());
queryValues.put("Phone", phn.getText().toString());
queryValues.put("Address", addr.getText().toString());
queryValues.put("Website", web.getText().toString());
queryValues.put("Blog", blog.getText().toString());
if(savefn.equals("")||savemail.equals("")||savemob.equals("")||saveaddr.equals("")&&saveln.equals("")&&saveweb.equals("")&&saveblog.equals("")) {
Toast.makeText(getApplicationContext(), "Fields vacant",1500).show();
return;
} else {
//dba.insertEntry(savefn, saveln, savemail, savemob, saveaddr, saveweb, saveblog);
controller.insertContact(queryValues);
Toast.makeText(getApplicationContext(), "Contact Saved Successfully" , 2000).show();
//dba.close();
return;
} // this.callHomeActivity(view);
}
});
}
public void addNewContact(View view) {
}
//public void callHomeActivity(View view) {
// Intent objIntent = new Intent(getApplicationContext(), ContactActivity.class);
// startActivity(objIntent);
}
**Edit Contact**
public class AddContact extends Activity{
Button btnadd;
EditText fname, lname, mail, phn,addr, web, blog;
DBContoller controller = new DBContoller(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
fname = (EditText) findViewById(R.id.editText1);
lname = (EditText) findViewById(R.id.editText2);
mail = (EditText) findViewById(R.id.editText3);
phn = (EditText) findViewById(R.id.editText4);
addr = (EditText) findViewById(R.id.editText5);
web = (EditText) findViewById(R.id.editText6);
blog = (EditText) findViewById(R.id.editText7);
btnadd = (Button) findViewById(R.id.btnadd);
btnadd.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getApplicationContext(), ContactActivity.class);
startActivity(objIntent);
String savefn = fname.getText().toString();
String saveln = lname.getText().toString();
String savemail = mail.getText().toString();
String savemob = phn.getText().toString();
String saveaddr = addr.getText().toString();
String saveweb = web.getText().toString();
String saveblog = blog.getText().toString();
HashMap<String, String> queryValues = new HashMap<String, String>();
queryValues.put("FirstName", fname.getText().toString());
queryValues.put("LastName", lname.getText().toString());
queryValues.put("Email", mail.getText().toString());
queryValues.put("Phone", phn.getText().toString());
queryValues.put("Address", addr.getText().toString());
queryValues.put("Website", web.getText().toString());
queryValues.put("Blog", blog.getText().toString());
if(savefn.equals("")||savemail.equals("")||savemob.equals("")||saveaddr.equals("")&&saveln.equals("")&&saveweb.equals("")&&saveblog.equals("")) {
Toast.makeText(getApplicationContext(), "Fields vacant",1500).show();
return;
} else {
//dba.insertEntry(savefn, saveln, savemail, savemob, saveaddr, saveweb, saveblog);
controller.insertContact(queryValues);
Toast.makeText(getApplicationContext(), "Contact Saved Successfully" , 2000).show();
//dba.close();
return;
} // this.callHomeActivity(view);
}
});
}
public void addNewContact(View view) {
}
//public void callHomeActivity(View view) {
// Intent objIntent = new Intent(getApplicationContext(), ContactActivity.class);
// startActivity(objIntent);
}
**DBController**
public class DBContoller extends SQLiteOpenHelper{
private static final String LOGCAT = null;
public DBContoller(Context applicationcontext) {
super(applicationcontext, "androidsqlite.db", null, 1);
Log.d(LOGCAT,"Created");
}
@Override
public void onCreate(SQLiteDatabase database) {
String query;
query = "CREATE TABLE CONTACT ( CId INTEGER PRIMARY KEY AUTOINCREMENT, FirstName TEXT, LastName TEXT, EMail TEXT, Mobile TEXT, Address TEXT, Website TEXT, Blog TEXT)";
database.execSQL(query);
Log.d(LOGCAT,"Contacts Created");
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
String query;
query = "DROP TABLE IF EXISTS CONTACT";
database.execSQL(query);
onCreate(database);
}
public void insertContact(HashMap<String, String> queryValues) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Ins_FirstName", queryValues.get("FirstName"));
values.put("Ins_LastName", queryValues.get("LastName"));
values.put("Ins_Email", queryValues.get("Email"));
values.put("Ins_Mobile", queryValues.get("Mobile"));
values.put("Ins_Address", queryValues.get("Address"));
values.put("Ins_Website", queryValues.get("Website"));
values.put("Ins_Blog", queryValues.get("Blog"));
database.insert("CONTACT", null, values);
database.close();
}
public int updateContact(HashMap<String, String> queryValues) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Upd_FirstName", queryValues.get("FirstName"));
values.put("Upd_LastName", queryValues.get("LastName"));
values.put("Upd_Email", queryValues.get("Email"));
values.put("Upd_Mobile", queryValues.get("Mobile"));
values.put("Upd_Address", queryValues.get("Address"));
values.put("Upd_Website", queryValues.get("Website"));
values.put("Upd_Blog", queryValues.get("Blog"));
return database.update("CONTACT", values, "CId" + " = ?", new String[] { queryValues.get("CId") });
}
public void deleteContact(String id) {
Log.d(LOGCAT,"delete");
SQLiteDatabase database = this.getWritableDatabase();
String deleteQuery = "DELETE FROM CONTACT where CId='"+ id +"'";
Log.d("query",deleteQuery);
database.execSQL(deleteQuery);
}
public ArrayList<HashMap<String, String>> getAllContact() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM CONTACT";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("CId", cursor.getString(0));
map.put("FirstName", cursor.getString(1));
wordList.add(map);
} while (cursor.moveToNext());
}
return wordList;
}
public HashMap<String, String> getContactInfo(String id) {
HashMap<String, String> wordList = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM CONTACT where CId='"+id+"'";
Cursor cursor = database.rawQuery(selectQuery,null);
if (cursor.moveToFirst()) {
do {
wordList.put("FirstName", cursor.getString(1));
wordList.put("LastName", cursor.getString(2));
wordList.put("Email", cursor.getString(3));
wordList.put("Mobile", cursor.getString(4));
wordList.put("Address", cursor.getString(5));
wordList.put("Website", cursor.getString(6));
wordList.put("Blog", cursor.getString(7));
} while (cursor.moveToNext());
}
return wordList;
}
}
Logcat错误; 01-17 20:32:34.651:D /(286):创建 01-17 20:33:13.581:D / AndroidRuntime(286):关闭VM 01-17 20:33:13.581:W / dalvikvm(286):threadid = 1:线程退出未捕获异常(组= 0x4001d800) 01-17 20:33:13.621:E / AndroidRuntime(286):致命异常:主要 01-17 20:33:13.621:E / AndroidRuntime(286):java.lang.NullPointerException 01-17 20:33:13.621:E / AndroidRuntime(286):at com.example.contact.ContactActivity $ 1.onItemClick(ContactActivity.java:41) 01-17 20:33:13.621:E / AndroidRuntime(286):在android.widget.AdapterView.performItemClick(AdapterView.java:284) 01-17 20:33:13.621:E / AndroidRuntime(286):在android.widget.ListView.performItemClick(ListView.java:3382) 01-17 20:33:13.621:E / AndroidRuntime(286):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:1696) 01-17 20:33:13.621:E / AndroidRuntime(286):在android.os.Handler.handleCallback(Handler.java:587) 01-17 20:33:13.621:E / AndroidRuntime(286):在android.os.Handler.dispatchMessage(Handler.java:92) 01-17 20:33:13.621:E / AndroidRuntime(286):在android.os.Looper.loop(Looper.java:123) 01-17 20:33:13.621:E / AndroidRuntime(286):在android.app.ActivityThread.main(ActivityThread.java:4627) 01-17 20:33:13.621:E / AndroidRuntime(286):at java.lang.reflect.Method.invokeNative(Native Method) 01-17 20:33:13.621:E / AndroidRuntime(286):at java.lang.reflect.Method.invoke(Method.java:521) 01-17 20:33:13.621:E / AndroidRuntime(286):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 01-17 20:33:13.621:E / AndroidRuntime(286):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-17 20:33:13.621:E / AndroidRuntime(286):at dalvik.system.NativeStart.main(Native Method) 01-17 20:33:17.341:I / Process(286):发送信号。 PID:286 SIG:9
答案 0 :(得分:0)
contactId = (TextView) view.findViewById(R.id.CId);
with:
TextView contactId = (TextView) view;