I'm creating a simple Android app so I can play around with a few cool things and test out how Android really works. So far I've been able to successfully incorporate the Navigation Drawer, multiple fragments in one activity, and a datasource connection. My issue is that when I'm trying to create a long click listener so I can delete items out of a list, the app "Unexpectedly Closes". I have 3 fragments named WeekFragment, MonthFragment, and CustomFragment.
public static WeekFragment newInstance(String param1, String param2) {
WeekFragment fragment = new WeekFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public WeekFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_week, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity)activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dataSource = new DataSource(getActivity());
dataSource.open();
trans = dataSource.getAllTransactions("Date");
itemAdapter = new ItemAdapter(getActivity(), R.layout.list_item, trans);
listView = (ListView) getView().findViewById(R.id.transactionListWeek);
listView.setLongClickable(true);
listView.setAdapter(itemAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long row_id) {
Toast.makeText(getActivity(), "PLS WORK", Toast.LENGTH_LONG).show();
return true;
}
});
}
The other 2 fragments deliver the same issue, I have not been able to see any errors in the log. (I'm using Android Studio).
Any help would be greatly appreciated!
Edit: Logcat Stack Trace
07-11 22:54:48.125 25117-25117/financialtracker.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: financialtracker.app, PID: 25117
java.lang.NullPointerException
at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onCreateActionMode(PhoneWindow.java:3005)
at com.android.internal.app.ActionBarImpl$ActionModeImpl.dispatchOnCreate(ActionBarImpl.java:1021)
at com.android.internal.app.ActionBarImpl.startActionMode(ActionBarImpl.java:550)
at android.app.Activity.onWindowStartingActionMode(Activity.java:5191)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onWindowStartingActionMode(ActionBarActivityDelegateICS.java:341)
at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2617)
at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2604)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
at android.view.View.startActionMode(View.java:4731)
at android.widget.AbsListView.performLongPress(AbsListView.java:3565)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3521)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
In order to get your ListView to handle long clicks as you have it setup, you'll need to change the following line:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
to:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Or, depending on how you plan to select and remove items, you might remove it completely.