Reputation: 1727
I am using one listview
to show details of places. For that i had created one CustomAdapter
but i don't know why it is giving me Null Pointer Exception
please help me to sort this problem.
This is my xml file.
activity_main.xml
<LinearLayout 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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
list_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="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img_hotel"
android:layout_width="100dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_hotelName"
android:layout_width="fill_parent"
android:layout_height="20dp" />
<TextView
android:id="@+id/txt_hotelAddress"
android:layout_width="fill_parent"
android:layout_height="20dp" />
<RatingBar
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2.0"
android:stepSize="0.5" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
This is my updated Custom Adapter
public class CustomListViewAdapter extends BaseAdapter {
Context context;
public CustomListViewAdapter(Context context, int resource,
List<Result> results) {
// super(context, resource, results);
this.context = context;
Log.v("Result Data in Adapter", results.toString());
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
Result result = (Result) getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// LayoutInflater mInflater = (LayoutInflater)
// context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.imgView = (ImageView) convertView
.findViewById(R.id.img_hotel);
holder.txtName = (TextView) convertView
.findViewById(R.id.txt_hotelName);
holder.txtAddress = (TextView) convertView
.findViewById(R.id.txt_hotelAddress);
holder.rate = (RatingBar) convertView
.findViewById(R.id.ratingBar);
convertView.setTag(holder);
} else {
/*
* holder = (ViewHolder) convertView.getTag();
*
* String url = result.icon; Log.e("URL is ", url); if (url !=
* null) { Bitmap bitmap = fetchBitmapFromCache(url); if (bitmap
* == null) { new
* BitmapDownloaderTask(holder.imgView).execute(url); } else {
* holder.imgView.setImageBitmap(bitmap); } } else {
* holder.imgView.setImageBitmap(null); }
*/
holder.imgView.setImageDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
holder.txtName.setText(result.name);
holder.txtAddress.setText(result.formatted_address);
holder.rate.setRating(result.rating);
}
return convertView; // super.getView(position, convertView, parent);
}
private class ViewHolder {
ImageView imgView;
TextView txtName;
TextView txtAddress;
RatingBar rate;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return results.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return results.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return results.indexOf(getItem(position));
}
}
And here i have set the adapter to listView
list.setAdapter(new CustomListViewAdapter(MainActivity.this,
R.layout.list_item, results));
This is my updated code upto now but it is still giving me null pointer exception.
and this is my stack strace.
03-13 11:13:20.355: E/AndroidRuntime(413): FATAL EXCEPTION: main
03-13 11:13:20.355: E/AndroidRuntime(413): java.lang.NullPointerException
03-13 11:13:20.355: E/AndroidRuntime(413): at com.th.hotelreservationsystem.MainActivity$CustomListViewAdapter.getView(MainActivity.java:183)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.ListView.onMeasure(ListView.java:1109)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.os.Looper.loop(Looper.java:123)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 11:13:20.355: E/AndroidRuntime(413): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 11:13:20.355: E/AndroidRuntime(413): at java.lang.reflect.Method.invoke(Method.java:521)
03-13 11:13:20.355: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 11:13:20.355: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 11:13:20.355: E/AndroidRuntime(413): at dalvik.system.NativeStart.main(Native Method)
Please help me friends.
Upvotes: 1
Views: 713
Reputation: 1727
Thanks for help guys.
I got the solution and this is my updated code with comments.
3 Mistakes of my code. Thnks to guys who helped me
public class CustomListViewAdapter extends BaseAdapter {
Context context;
public CustomListViewAdapter(Context context, int resource,
List<Result> results) {
super(); // 1) here i was trying to call parameterised Constructor which
// was wrong.
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
Result result = (Result) getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder(); // 2) i had not instantiated View holder
// here i did.
holder.imgView = (ImageView) convertView
.findViewById(R.id.img_hotel);
holder.txtName = (TextView) convertView
.findViewById(R.id.txt_hotelName);
holder.txtAddress = (TextView) convertView
.findViewById(R.id.txt_hotelAddress);
holder.rate = (RatingBar) convertView
.findViewById(R.id.ratingBar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();// 3) here by mistake i
//had commited holde with other unwanted code. here removed it.
holder.imgView.setImageDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
holder.txtName.setText(result.name);
holder.txtAddress.setText(result.formatted_address);
holder.rate.setRating(result.rating);
}
return convertView; // super.getView(position, convertView, parent);
}
private class ViewHolder {
ImageView imgView;
TextView txtName;
TextView txtAddress;
RatingBar rate;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return results.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return results.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return results.indexOf(getItem(position));
}
}
Upvotes: 2
Reputation: 46
You are setting your viewHolder to null in the starting of getView. You initialized it if convertView is null. But if convertView is not null, you used it directly. Don't forget viewHolder is null if convertView is not null.
Add following code in the starting of else block:
holder = (ViewHolder) convertView.getTag();
Upvotes: 3
Reputation: 2395
@Mahaveer Can you please make it
@Override
public int getCount() {
return <Size of list items>;
}
hope this will fix your issue.
Upvotes: 1
Reputation: 4119
You should return convertView in your getView method instead of calling:
return super.getView(position, convertView, parent); // replace this line by return convertView;
EDIT: So full answer would be something like this:
public class CustomListViewAdapter extends ArrayAdapter<Result>{
Context context;
public CustomListViewAdapter(Context context, int resource,
List<Result> results) {
super(context, resource, results);
this.context = context; // 1
Log.v("Result Data in Adapter", results.toString());
// TODO Auto-generated constructor stub
}
private class ViewHolder {
ImageView imgView;
TextView txtName;
TextView txtAddress;
RatingBar rate;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
Result result = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null ){
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder(); //2
// holder.imgView = (ImageView)convertView.findViewById(R.id.img_hotel);
holder.txtName = (TextView) convertView.findViewById(R.id.txt_hotelName);
holder.txtAddress = (TextView) convertView.findViewById(R.id.txt_hotelAddress);
holder.rate = (RatingBar)convertView.findViewById(R.id.ratingBar);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
//holder.imgView.setImageResource();
holder.txtName.setText(result.name);
holder.txtAddress.setText(result.formatted_address);
holder.rate.setRating(result.rating);
}
return convertView; //3
}
}
Upvotes: 1
Reputation: 9778
You are setting your ViewHolder
to null
, but never initializing it.
So make your getView()
like this:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
Result result = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null ){
holder = new viewHolder(); //initialize it
convertView = mInflater.inflate(R.layout.list_item, null);
....
convertView.setTag(holder);
}...
Upvotes: 0
Reputation: 20155
Your context
is null as you have not initialize it in the constructor,
try to initialize it like this
public CustomListViewAdapter(Context context, int resource,
List<Result> results) {
super(context, resource, results);
this.context=context;
}
And also your viewholder is null
Try to initialize your view holder
holder = new viewHolder();
Upvotes: 1