java null指针异常数据库

时间:2011-12-09 20:25:04

标签: android

我有一个数据库联系人应用程序。当我在列表视图中单击一个联系人时,我想以另一个意图显示我的联系人详细信息。 这是我的.java代码:`

          package org.example.dbcontactconsole;
          import android.app.ListActivity;
          import android.content.Intent;
          import android.database.Cursor;
          import android.database.sqlite.SQLiteDatabase;
          import android.os.Bundle;
          import android.view.View;
          import android.view.View.OnClickListener;
          import android.widget.Button;
          import android.widget.EditText;
          import android.widget.ListAdapter;
          import android.widget.ListView;
          import android.widget.SimpleCursorAdapter;
          import static android.provider.BaseColumns._ID;

public class Search extends  ListActivity {

private static int[] TO = {R.id.rowid,R.id.name, R.id.mobilephone, R.id.email };  
 private static String[] FROM = {_ID,DbConstants.NAME, DbConstants.PHONE,DbConstants.EMAIL, };


     private Button sButton;
 private ListView lv1;
 private static SQLiteDatabase db;
  private DbCreate contacts;
  private Cursor cursor;
  private EditText searchText;
  protected ListAdapter adapter;

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    searchText=(EditText)findViewById(R.id.searchText);
    sButton=(Button)findViewById(R.id.searchButton);
    sButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDatabaseContent();
              lv1 = getListView();
              lv1.setTextFilterEnabled(true);
        }
    }); 
}
  private Cursor getContacts() {
      db = contacts.getReadableDatabase();
      cursor = db.rawQuery("SELECT  _id,name, phone, email FROM contactTest1 WHERE name LIKE ?", 
                new String[]{searchText.getText().toString()+"%"});
      startManagingCursor(cursor);
      return cursor;}

        public void showDatabaseContent(){
   contacts = new DbCreate(this); 
   try {
       cursor = getContacts(); 
       showContacts(cursor); 
   } finally {
       contacts.close(); 
       db.close();
   }
 }

   private void showContacts(Cursor cursor) {
   //set up data binding
   SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
   setListAdapter(adapter);
  }
public void onListItemClick(ListView parent, View v, int position, long id) {
   Intent abaintent = new Intent(this,Detalii.class);
   Cursor cursor = (Cursor) adapter.getItem(position);
   abaintent.putExtra("Contact_ID", cursor.getInt(cursor.getColumnIndex("_id")));
   startActivity(abaintent);
  } 
  }

Here is Detalii.java:      package org.example.dbcontactconsole;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class Detalii extends Activity
          { 
 protected TextView contactName;
 protected TextView contactPhone;
 protected TextView email;
 protected int contactId;


 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.detalii);

 contactId = getIntent().getIntExtra("Contact_ID",0);
 SQLiteDatabase db = (new DbCreate(this)).getWritableDatabase();
 Cursor cursor = db.rawQuery("SELECT name,phone,email FROM contactTest1 WHERE   _id=?",new String[]{""+contactId});

 if (cursor.getCount() == 1)
{
    cursor.moveToFirst();

    contactName = (TextView) findViewById(R.id.name);
    contactName.setText(cursor.getString(cursor.getColumnIndex("name")));



    contactPhone = (TextView) findViewById(R.id.phone);
    contactPhone.setText(cursor.getString(cursor.getColumnIndex("phone")));



    email = (TextView) findViewById(R.id.email);
    email.setText(cursor.getString(cursor.getColumnIndex("email")));

     }

  }


  }

`

我得到了:`

       java.lang.NullPointerException

       at org.example.dbcontactconsole.Search.onListItemClick(Search.java:80)
       at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
       at android.app.ActivityThread.main(ActivityThread.java:3683)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:507)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
       (ZygoteInit.java:839)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
       at dalvik.system.NativeStart.main(Native Method)

`

1 个答案:

答案 0 :(得分:0)

您没有初始化实例变量adapter,因此当您尝试使用null方法访问它时,它的值为onListItemClick。如果您将实例变量adapter(顶部的那个)声明为SimpleCursorAdapter而不是ListAdapter,我认为您将获得所需的结果,然后更改此行来自:

 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);

 adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);