I am new to Android App. I am working on a app which needs to display image and text in listView. For that I had extend ListActivity. In that I had used EfficientAdapter extending BaseAdapter. I had setted the OnItemClickListner too. But When I try to open an New Screen while clicking on tany item my app trow a NullPointer exception in intent and startActivity Method.
public class MyClass extends ListActivity {
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setListAdapter(new StationAdapter(this));
..
}
private static class StationAdapter extends BaseAdapter {
static LayoutInflater mYInflater;
public StationAdapter(Context context) {
mYInflater = LayoutInflater.from(context);
}
public int getCount() {
return MyArray.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = myInflater.inflate(R.layout.customlister, null);
holder.title = (TextView)convertView.findViewById(R.id.myTitle);
holder.icon = (ImageView)convertView.findViewById(R.id.myIcon);
}
else {
holder 开发者_C百科= (ViewHolder)convertView.getTag();
}
holder.title.setText(MyArray[position]);
holder.icon.setImageBitmap(iconImages[position]);
convertView.setOnClickListener(new OnItemClickListener(position));
return convertView;
}
static class ViewHolder {
TextView title;
ImageView icon;
}
private class OnItemClickListener implements OnClickListener {
private int mPosition;
OnItemClickListener(int position) {
mPosition = position;
}
public void onClick(View view) {
try {
System.out.println("Clicked On Row - = " + mPosition);
Intent i = new Intent(MyCurrentClass.this,MyNewClass.class);
startActivity(i);
}
catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
}
}
}
The following two lines:
Intent i = new Intent(MyCurrentClass.this,MyNewClass.class);
startActivity(i);
Works fine If I directly write these in My Class which extend ListActivity. But its not working here. I had tried to set My Intent in OnCreate Method and then create a function:
public void GoToNextScreen() {
try {
startActivity(i);
}
catch (Exception e) {
System.out.println("XML Parsing Exception = " + e);
}
}
Then in my OnItemClickListener implementation I had called this method but still exception. Please help me out how can I move on new screen. Any Help will be greatly appreciated.
Thanks in advance.
The reason you are getting a NullPointerException on your startActivity(i) is because that method is not defined in your private class OnItemClickListener implements OnClickListener
. It works fine in MyClass because startActivity is defined in the Activity class (from which ListActivity exends from).
To make your current code work i would do the following:
Intent i = new Intent(MyClass.this,MyNewClass.class);
MyClass.this.startActivity(i);
As startActivity is defined in the Activity class (from which ListActivity exends from), you need to reference that class to be able to perform that method from inner classes.
Hope this helps!
P.S. i noticed that you have a typo in the code, as you have mYInflater
and myInflater
in your StationAdapter
.
精彩评论