Reputation: 417
I just want to pass a variable that I get from cursor to Service. But always get a NullPointerException
.
This is my adapter, which I use to show data into listView.
public class DetailCategoryCursorAdapter extends CursorAdapter{
private Services services;
public DetailCategoryCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item_detail, parent, false);
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
TextView titleTextView = (TextView) view.findViewById(R.id.title);
ImageView imageView = (ImageView) view.findViewById(R.id.dplay);
int titleColumnIndex = cursor.getColumnIndex(Contract.Entry.COLUMN_TITLE);
int urlColumnIndex = cursor.getColumnIndex(Contract.Entry.COLUMN_URL);
String title = cursor.getString(titleColumnIndex);
String url = cursor.getString(urlColumnIndex);
nameTextView.setText(title);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
Intent sIntent = new Intent(context, Services.class);
sIntent.putExtra("url", url);
services.startService(sIntent); // in this part always show NullPointerEception
Log.v("url", url);
}catch (Exception e){
e.printStackTrace();
}
}
});
}
}
Or is there a better way to do it? Need advice. Thanks
Upvotes: 2
Views: 96
Reputation: 11487
Use this in your onBind
it wont throw the exception. (because your services
is null)
Intent sIntent = new Intent(context, Services.class);
sIntent.putExtra("url", url);
context.startService(sIntent);
Upvotes: 1