Android长按监听器

时间:2014-07-12 01:19:45

标签: android android-fragments onitemlongclicklistener

我正在创建一个简单的Android应用程序,因此我可以玩一些很酷的东西,并测试Android的真正运作方式。到目前为止,我已经能够成功地将导航抽屉,多个片段合并到一个活动中,以及数据源连接。我的问题是,当我尝试创建一个长时间点击监听器,以便我可以从列表中删除项目时,应用程序“意外关闭”。我有3个名为WeekFragment,MonthFragment和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;
        }
    });
}

其他2个片段提供相同的问题,我无法在日志中看到任何错误。 (我正在使用Android Studio)。

非常感谢任何帮助!

编辑:Logcat堆栈跟踪

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)

2 个答案:

答案 0 :(得分:2)

为了让ListView在设置时能够处理长时间点击,您需要更改以下行:

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

为:

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

或者,根据您计划选择和删除项目的方式,您可以完全删除它。

答案 1 :(得分:0)

如果您想使用CHOICE_MODE_MULTIPLE_MODAL,则需要实施MultiChoiceModeListener并致电setMultiChoiceModeListener()。有关示例,请参阅我的CheckableLinearLayout