Null Pointer When StartActivity on Grid Layout Adapter
-
13-11-2019 - |
سؤال
I have a button inside grid layout adapter, and I want to set OnClickListener in getView method to start activity, But I have Null Pointer Exception when I click it.. This is my code:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
LoaderImageView thumnail=new LoaderImageView(mCon, "");
LinearLayout thum=(LinearLayout)convertView.findViewById(R.id.thumn);
thumnail.setImageDrawable(mList.get(position).getPict());
thum.addView(thumnail);
TextView namaPro=(TextView)convertView.findViewById(R.id.namaPro);
namaPro.setText(mList.get(position).getNama());
/*Rate* Menyusul*/
Button view=(Button)convertView.findViewById(R.id.viewlist);
view.setId(position);
view.setOnClickListener(new onClick());
return convertView;
}
class onClick extends Activity implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(mCon, DetailProduct.class);
startActivity(i);
}
}
Anything wrong with my code above??
المحلول
In the onclicklistener try calling startActivity() with the Context instance. The adapter will not have a context by itself. So you have to specify the context for starting the activity.
class onClick extends Activity implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(mCon, DetailProduct.class);
mCon.startActivity(i);
}
}
لا تنتمي إلى StackOverflow