Issue
Today I encountered a Strange Problem in my app. SO in first activity, there is a navigation drawer with some items listed in listview (in drawer).there is scrollbar in navigation drawer. now, on some random devices Problems occurs. App is launching correctly but as soon as I scroll ( Inside Navigation drawer) , app is crashed. Eclipse is throwing NullPointer Exception. I dont get it because the problem occurs on some devices but in others it works perfectly. I'm posting logcat as well as other related files.
Logcat:
02-16 18:54:46.879: E/AndroidRuntime(1654): FATAL EXCEPTION: main
02-16 18:54:46.879: E/AndroidRuntime(1654): Process: com.example.passpass, PID: 1654
02-16 18:54:46.879: E/AndroidRuntime(1654): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
02-16 18:54:46.879: E/AndroidRuntime(1654): at com.example.passpass.adapter.NavDrawerListAdapter.getView(NavDrawerListAdapter.java:89)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.widget.AbsListView.obtainView(AbsListView.java:2344)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.widget.ListView.makeAndAddView(ListView.java:1864)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.widget.ListView.fillDown(ListView.java:698)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.widget.ListView.fillGap(ListView.java:662)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4968)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3398)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.widget.AbsListView.onTouchMove(AbsListView.java:3774)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3612)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.View.dispatchTouchEvent(View.java:8388)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2424)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
02-16 18:54:46.879: E/AndroidRuntime(1654): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
02-16 18:54:46.879: E/AndroidRuntime(1654): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
02-16 18:54:46.879: E/AndroidRuntime(1654): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.View.dispatchPointerEvent(View.java:8578)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5762)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5814)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.Choreographer.doFrame(Choreographer.java:548)
02-16 18:54:46.879: E/AndroidRuntime(1654): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
02-16 18:54:46.879: E/AndroidRuntime(1654): at
Adapter's Getview method Logcat is talking about
public class NavDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<Category> navDrawerItems;
public NavDrawerListAdapter(Context context, ArrayList<Category> navDrawerItems){
this.context = context;
this.navDrawerItems = navDrawerItems;
}
@Override
public int getCount() {
return navDrawerItems.size();
}
@Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
int type=3;
if(navDrawerItems.get(position).getId()==0)
type=0;
else if(navDrawerItems.get(position).getId()==10)
type=10;
else
type=1;
return type;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
if(type==0)
{
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(navDrawerItems.get(position).getName());
}
else if(type==1)
{
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item_sub, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(navDrawerItems.get(position).getName()); //Eclipse is throwing NPE here
}
else
{
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.home, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.home);
txtTitle.setText(navDrawerItems.get(position).getName());
}
return convertView;
}
}
XML file: This is one of three layouts I'm using for different rows of Navigation Drawer.
<?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="48dp"
android:background="@drawable/list_selector">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="55dp"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/list_item_title"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:textSize="10dp"/>
</RelativeLayout>
What's causing the problem? Please tell me if you want me to post navigation drawer code or layout file.
Thank You In Advance
Solution
You seem to be using three different item types, and you're respecting convertView
. This means that you might be handed a different layout by the OS than you are expecting back, as it's being recycled. For example, you might get the home
layout back, which does not appear to have a title
view.
To correct that, you either need to keep track of which type of layout you have inflated, for example, by adding a tag
and/or a viewholder pattern; or you need to ignore the recycled view and always inflate the views.
Answered By - 323go
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.