2012-11-28 39 views
1

我想將數據從我的數據庫DatabaseBon改爲ListView。我想要這樣做,而不使用ListViewActivityListView和cursorAdapter給出了一個空指針Exceptiom

我發現我應該使用cursorAdapter這個,但有沒有其他方式(沒有ListViewActivity)?我實現了CursorAdapter,但得到了空指針異常。

我的活動是這樣的:

 public class BlackNumbersBlockDb extends Activity { 
     String TAG; 
     @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.blocknumbersdb); 
    @SuppressWarnings("unused") 
    Button imBT= (Button)findViewById(R.id.importBL); 
    DataBaseBON info= new DataBaseBON(this); 
    info.open(); 
    Cursor cursor=info.getcursor(); 
    startManagingCursor(cursor); 
    ContactCursorAdapter adapter = new ContactCursorAdapter(null, cursor); 
    ListView contactLV = (ListView) findViewById(R.id.listviewblDB); 
    contactLV.setAdapter(adapter); 
    info.close(); 
     imBT.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i= new Intent ("android.intent.action.IMPORTM"); 
      startActivity(i); 
     } 
    }); 

我CursorAdapter的樣子:

  import vahid.engineer.com.DataBaseBON 
      import android.content.Context; 
      import android.database.Cursor; 
      import android.view.LayoutInflater; 
    public class ContactCursorAdapter extends CursorAdapter { 
    public ContactCursorAdapter(Context context, Cursor c) { 
    super(context, c); 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView name = (TextView)view.findViewById(R.id.blacklistDB1); 
    name.setText(cursor.getString(cursor.getColumnIndex(DataBaseBON.N_NAME))); 
    TextView phone = (TextView)view.findViewById(R.id.blacklistDB2); 
    phone.setText(cursor.getString(cursor.getColumnIndex(DataBaseBON.KEY_NUMBER)));} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    LayoutInflater inflater = LayoutInflater.from(context); 
    View v = inflater.inflate(R.layout.lv, parent, false); 
    bindView(v, context, cursor); 
    return v; 

我的XML文件(塊與否)是看起來像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/listviewblDB" 
     android:layout_width="match_parent" 
     android:layout_height="499dp" 
     android:layout_weight="0.72" > 
    </ListView> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="129dp" 
    android:gravity="bottom|center_horizontal|fill|top" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/importBL" 
     android:layout_width="100dp" 
     android:layout_height="90dp" 
     android:layout_marginLeft="10sp" 
     android:layout_marginTop="20sp" 
     android:ems="10" 
     android:text="Import" 
     android:textSize="20sp"/> 

    </LinearLayout> 

我的XML文件LV看起來像:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
      android:id="@+id/blacklistDB1" 
      android:layout_width="33dp" 
      android:layout_height="wrap_content" 
      android:text="Large Text" 
      /> 
    <TextView 
     android:id="@+id/blacklistDB2" 
     android:layout_width="19dp" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     /> 
    </LinearLayout> 

我的錯誤看起來像

FATAL EXCEPTION: main 
    java.lang.NullPointerException 
at android.view.LayoutInflater.from(LayoutInflater.java:171) 
at vahid.engineer.com.ContactCursorAdapter.newView 
at android.widget.CursorAdapter.getView(CursorAdapter.java:182) 
at android.widget.AbsListView.obtainView(AbsListView.java:1315) 
at android.widget.ListView.makeAndAddView(ListView.java:1727) 
    at android.widget.ListView.fillDown(ListView.java:652) 
at android.widget.ListView.fillFromTop(ListView.java:709) 
at android.widget.ListView.layoutChildren(ListView.java:1580) 
at android.widget.AbsListView.onLayout(AbsListView.java:1147) 
at android.view.View.layout(View.java:7035) 
at android.widget.LinearLayout.setChildFrame(LinearLayout.java: 
at android.widget.LinearLayout.layoutVertical(LinearLayout.java: 
at android.widget.LinearLayout.onLayout(LinearLayout.java:1042) 
at android.view.View.layout(View.java:7035) 

回答

1
ContactCursorAdapter adapter = new ContactCursorAdapter(null, cursor); 

,看起來它會扯上它。該參數將調用您的活動的上下文,而不是null。你爲數據庫實例化做得很好。以下應該工作。

ContactCursorAdapter adapter = new ContactCursorAdapter(BlackNumbersBlockDb.this, cursor); 
+0

多數民衆贊成在偉大的答案,非常感謝你,你不能想象你給了我什麼大的幫助,最好的祝願你 –