Below class extends from
public class LocationListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Location>>{
private final static String TAG = LocationListFragment.class.getName();
private LocationListLoader locationListLoader;
private LocationListAdapter locationListAdapter;
private int userId;
Context context;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getActivity(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
Log.i("position",""+position);
}
});
int locationListType = getArguments().getInt(LocationListActivityFinal.LOCATION_LIST_TYPE);
switch (locationListType) {
case LocationListActivityFinal.DOMESTIC:
userId = getArguments().getInt(LocationListActivityFinal.ASSIGNED_USER_ID);
break;
case LocationListActivityFinal.COMMERCIAL:
userId = getArguments().getInt(LocationListActivityFinal.ASSIGNED_USER_ID);
break;
default:
break;
}
getLoaderManager().initLoader(101,null,this);
}
@Override
public void onListItemClick(ListView l,View v, int position, long id) {
Log.i("DataListFragment", "Item clicked: " + id);
}
@Override
public Loader<List<Location>> onCreateLoader(int id, Bundle args) {
locationListLoader = new LocationListLoader(getActivity());
return locationListLoader;
}
@Override
public void onLoadFinished(Loader<List<Location>> arg0, List<Location> locations) {
locationListAdapter = new LocationListAdapter(getActivity(),locationListLoader);
setListAdapter(locationListAdapter);
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
locationListAdapter.notifyDataSetChanged();
}
}
I am filling ListView from Service and its working fine but when trying to click on ListView its showing this LogCat:
01-14 04:21:29.995: E/AndroidRuntime(3174): FATAL EXCEPTION: main
01-14 04:21:29.995: E/AndroidRuntime(3174):
java.lang.ClassCastException: android.widget.LinearLayout cannot be
cast to android.widget.TextView 01-14 04:21:29.995:
E/AndroidRuntime(3174): at
com.mrfs.android.surveyapp.activities.fragments.LocationListFragment$1.onItemClick(LocationListFragment.java:45)
01-14 04:21:29.995: E/AndroidRuntime(3174): at
android.widget.AdapterView.performItemClick(AdapterView.java:301)
01-14 04:21:29.995: E/AndroidRuntime(3174): at
android.widget.AbsListView.performItemClick(AbsListView.java:1276)
01-14 04:21:29.995: E/AndroidRuntime(3174): at
android.widget.AbsListView$PerformClick.run(AbsListView.java:3067)
01-14 04:21:29.995: E/AndroidRuntime(3174): at
android.widget.AbsListView$1.run(AbsListView.java:3963) 01-14
04:21:29.995: E/AndroidRuntime(3174): at
android.os.Handler.handleCallback(Handler.java:615) 01-14
04:21:29.995: E/AndroidRuntime(3174): at
android.os.Handler.dispatchMessage(Handler.java:92) 01-14
04:21:29.995: E/AndroidRuntime(3174): at
android.os.Looper.loop(Looper.java:137) 01-14 04:21:29.995:
E/AndroidRuntime(3174): at
android.app.ActivityThread.main(ActivityThread.java:4898) 01-14
04:21:29.995: E/AndroidRuntime(3174): at
java.lang.reflect.Method.invokeNative(Native Method) 01-14
04:21:29.995: E/AndroidRuntime(3174): at
java.lang.reflect.Method.invoke(Method.java:511) 01-14 04:21:29.995:
E/AndroidRuntime(3174): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-14 04:21:29.995: E/AndroidRuntime(3174): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 01-14
04:21:29.995: E/AndroidRuntime(3174): at
dalvik.system.NativeStart.main(Native Method)
Error when trying to Scroll ListView:
01-14 04:35:02.555: E/AndroidRuntime(6964): FATAL EXCEPTION: main 01-14 04:35:02.555: E/AndroidRuntime(6964): java.lang.NullPointerException 01-14 04:35:02.555: E/AndroidRuntime(6964): at com.mrfs.android.surveyapp.adapter.LocationListAdapter.getView(LocationListAdapter.java:91) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.widget.AbsListView.obtainView(AbsListView.java:2445) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.widget.ListView.makeAndAddView(ListView.java:1775) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.widget.ListView.fillDown(ListView.java:678) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.widget.ListView.fillGap(ListView.java:642) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5525) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3413) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3901) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.View.dispatchTouchEvent(View.java:7337) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2410) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2145) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159) 01-14 04:35:02.555: E/AndroidRuntime(6964): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2115) 01-14 04:35:02.555: E/AndroidRuntime(6964): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1468) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.app.Activity.dispatchTouchEvent(Activity.java:2487) 01-14 04:35:02.555: E/AndroidRuntime(6964): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2063) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.View.dispatchPointerEvent(View.java:7520) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4452) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4503) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.Choreographer.doCallbacks(Choreographer.java:555) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.Choreographer.doFrame(Choreographer.java:523) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.os.Handler.handleCallback(Handler.java:615) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.os.Handler.dispatchMessage(Handler.java:92) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.os.Looper.loop(Looper.java:137) 01-14 04:35:02.555: E/AndroidRuntime(6964): at android.app.ActivityThread.main(ActivityThread.java:4898) 01-14 04:35:02.555: E/AndroidRuntime(6964): at java.lang.reflect.Method.invokeNative(Native Method) 01-14 04:35:02.555: E/AndroidRuntime(6964): at java.lang.reflect.Method.invoke(Method.java:511) 01-14 04:35:02.555: E/AndroidRuntime(6964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 01-14 04:35:02.555: E/AndroidRuntime(6964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 01-14 04:35:02.555: E/AndroidRuntime(6964): at dalvik.system.NativeStart.main(Native Method)
Your exception states this:
So I guess this would be the row that's causing the Exception. The view is not a TextView, but a linearlayout. Try using view.findViewById(R.id.theTextViewIdFromALayoutFile) instead. And cast this to a TextView.