我創建了一個項目來顯示帶自定義行佈局的列表。點擊列表項後,調用一個活動,它從sqlite中檢索數據,並使用自定義行在列表視圖中顯示。但是,我在自定義適配器上的空指針異常
DisplayCards.java做setadapter時獲得空指針異常
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class DisplayCards extends Activity{
private Divyadesamdb dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new Divyadesamdb(this);
dbHelper.open();
// Clean all data
dbHelper.deleteAllPasurams();
// Add some data
dbHelper.insertPasurams();
// Generate ListView from SQLite Database
displayListView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void displayListView() {
String category = null;
String starting = "25";
String ending = "30";
int rowsFound = 0;
Cursor cursor = dbHelper.fetchAllPasurams(category,starting,ending);
/*
* How many rows returned from the query
*/
rowsFound = cursor.getCount();
Log.d("VC", "Query returned "+rowsFound+" rows.");
// the desired columns to be bound
String[] columns = new String[] { Divyadesamdb.PASURAMS_COLUMN_PAASURAMNUMBER,
Divyadesamdb.PASURAMS_COLUMN_AAYIRAM, Divyadesamdb.PASURAMS_COLUMN_AZHWAAR,
Divyadesamdb.PASURAMS_COLUMN_CATEGORY, Divyadesamdb.PASURAMS_COLUMN_MANGALASASANAMON,
Divyadesamdb.PASURAMS_COLUMN_PAASURAM_EN_STR, Divyadesamdb.PASURAMS_COLUMN_SUBCATEGORY,
Divyadesamdb.PASURAMS_COLUMN_TITLE, Divyadesamdb.PASURAMS_COLUMN_TITLENUMBER };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.tvPaasuramNumber, R.id.tvAayiram, R.id.tvAzhwaar,
R.id.tvCategory, R.id.tvMangalasasanamOn, R.id.tvPaasuram,
R.id.tvSubCategory, R.id.tvTitle, R.id.tvtvTitleNumber};
Log.d("VC","Just before creating dataAdapter");
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
MyCursorAdapter dataAdapter = new MyCursorAdapter(this,
R.layout.paasuram_single_item, cursor, columns, to, 0);
Log.d("VC","Just after creating dataAdapter");
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
Log.d("VC","created listView");
int myCount = dataAdapter.getCount();
Log.d("VC","got data from dataAdapter "+myCount);
//listView.getAdapter();
listView.setAdapter(dataAdapter);
Log.d("VC","completed setadapter dataAdapter");
}
// extend the SimpleCursorAdapter to create a custom class where we
// can override the getView to change the row colors
private class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get reference to the row
View view = super.getView(position, convertView, parent);
// check for odd or even to set alternate colors to the row
// background
if (position % 2 == 0) {
view.setBackgroundColor(Color.rgb(238, 233, 233));
} else {
view.setBackgroundColor(Color.rgb(255, 255, 255));
}
return view;
}
}
}
paasuram_single_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/mybackground"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivAzhwaar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_margin="10dp"
android:contentDescription="@string/Azhwaar"
android:padding="5dp"
android:src="@drawable/nammalwar" />
<TextView
android:id="@+id/tvtvTitleNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/ivAzhwaar"
android:layout_toRightOf="@+id/ivAzhwaar"
android:text="TitleNumber"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvHyphan1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvtvTitleNumber"
android:layout_alignBottom="@+id/tvtvTitleNumber"
android:layout_toRightOf="@+id/tvtvTitleNumber"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBaseline="@+id/tvHyphan1"
android:layout_alignBottom="@+id/tvHyphan1"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/tvHyphan1"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvAayiram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvtvTitleNumber"
android:layout_below="@+id/tvtvTitleNumber"
android:text="Aayiram"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvHyphan2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvtvTitleNumber"
android:layout_toRightOf="@+id/tvAayiram"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvHyphan2"
android:layout_alignBottom="@+id/tvHyphan2"
android:layout_toRightOf="@+id/tvHyphan2"
android:text="Category"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvHyphan3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvCategory"
android:layout_alignBottom="@+id/tvCategory"
android:layout_toRightOf="@+id/tvCategory"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvSubCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvHyphan3"
android:layout_alignBottom="@+id/tvHyphan3"
android:layout_toRightOf="@+id/tvHyphan3"
android:text="SubCategory"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvPaasuram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvAayiram"
android:layout_below="@+id/tvAayiram"
android:padding="5dp"
android:text="paasuram"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvPaasuramNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvPaasuram"
android:layout_alignBottom="@+id/tvPaasuram"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="PaasuramNumber"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvAzhwaar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvMangalasasanamOn"
android:layout_alignBottom="@+id/tvMangalasasanamOn"
android:layout_alignLeft="@+id/tvPaasuram"
android:text="Azhwaar"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvMangalasasanamOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvCategory"
android:layout_below="@+id/tvPaasuram"
android:layout_centerHorizontal="true"
android:text="MangalasasanamOn"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
paasuram_list_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
NPE是發生在DisplayCards.java
listView.setAdapter(dataAdapter);
能夠得到正確的計數的DataAdapter
任何幫助表示讚賞。
後'activity_main.xml' – Raghunandan