当点击碎片时,无法找到方法onClick。

5
我正在尝试创建一份待办事项清单。我承认我还在学习如何使用Fragment,但我认为我的问题是与Fragment的设计有冲突。现在我有一个任务的Fragment,在那里我将添加任务,并且下面有一个列表。当我键入时,我会点击添加按钮,然后它应该显示在列表上。我还有一个list_inner_view,其中有一个复选框。
现在,在XML中,我已经调用了android:onClick="addTaskNow"方法。
以下是我的代码:
我的fragment_tasks.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentTasks" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="14dp"
        android:text="Add" 
        android:onClick="addTaskNow"/>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1" >
    </ListView>

</RelativeLayout>

我的FragmentTasks.java

package com.example.navbar;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 * 
 */
public class FragmentTasks extends Fragment {

    public FragmentTasks() {
        // Required empty public constructor
    }

    protected TaskerDbHelper db;
    List<Task> list;
    MyAdapter adapt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_tasks, container, false);
        //return inflater.inflate(R.layout.fragment_tasks, container, false);
        db = new TaskerDbHelper(getActivity());
        list = db.getAllTasks();
        adapt = new MyAdapter(getActivity(), R.layout.list_inner_view, list);
        ListView listTask = (ListView) rootView.findViewById(R.id.list);
        listTask.setAdapter((ListAdapter) adapt);

        Button b = (Button) rootView.findViewById(R.id.button1);
        return rootView;
    }


    public void addTaskNow(View v) {
        EditText t = (EditText) v.findViewById(R.id.editText1);


        String s = t.getText().toString();
        if (s.equalsIgnoreCase("")) {
            Toast.makeText(getActivity(), "enter the task description first!!",
                    Toast.LENGTH_LONG);
        } else {
            Task task = new Task(s, 0);
            db.addTask(task);
            Log.d("tasker", "data added");
            t.setText("");
            adapt.add(task);
            adapt.notifyDataSetChanged();
        }

    }

    private class MyAdapter extends ArrayAdapter<Task> {

        Context context;
        List<Task> taskList = new ArrayList<Task>();
        int layoutResourceId;

        public MyAdapter(Context context, int layoutResourceId,
                List<Task> objects) {
            super(context, layoutResourceId, objects);
            this.layoutResourceId = layoutResourceId;
            this.taskList = objects;
            this.context = context;
        }

        /**
         * This method will DEFINe what the view inside the list view will
         * finally look like Here we are going to code that the checkbox state
         * is the status of task and check box text is the task name
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CheckBox chk = null;
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.list_inner_view,
                        parent, false);
                chk = (CheckBox) convertView.findViewById(R.id.checkBox1);
                convertView.setTag(chk);

                chk.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Task changeTask = (Task) cb.getTag();
                        changeTask.setStatus(cb.isChecked() == true ? 1 : 0);
                        db.updateTask(changeTask);
                        /*Toast.makeText(
                                getApplicationContext(),
                                "Clicked on Checkbox: " + cb.getText() + " is "
                                        + cb.isChecked(), Toast.LENGTH_LONG)
                                .show();
                    } */

                }});
            } else {
                chk = (CheckBox) convertView.getTag();
            }
            Task current = taskList.get(position);
            chk.setText(current.getTaskName());
            chk.setChecked(current.getStatus() == 1 ? true : false);
            chk.setTag(current);
            Log.d("listener", String.valueOf(current.getId()));
            return convertView;


    }




}}
2个回答

14

方法 "addTaskNow" 必须在添加片段的活动中。如果您想让片段处理点击事件,则需要将点击侦听器转发到片段,或在片段内设置点击侦听器。onClick仅适用于活动,而不是片段。


感谢您的迅速回复。所以我实现了OnClickListner并且需要添加未实现的方法。它创建了一个onClick(View v)方法。我删除了addTaskNow方法,并将其中的内容复制粘贴到OnClick方法中。但是它不起作用。您能否再详细解释一下我需要做什么。谢谢! - Adilp
你能详细说明一下“它不起作用”的意思吗?是应用程序崩溃了吗?还是方法没有执行? - Lena Bru

5

我一直觉得在布局文件中使用android:onClick属性使代码难以导航。你的代码不起作用的原因很可能是@Lena Bru的答案(该方法需要在活动中)。

我建议你按照以下步骤进行操作:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // ...

    Button b = (Button) rootView.findViewById(R.id.button1);
    b.setOnClickListener(mButtonClickListener);
    return rootView;
}

private OnClickListener mButtonClickListener = new OnClickListener() {
    public void onClick(View v) {
        addTaskNow();
    }
}

然后从fragment_tasks.xml中删除android:onClick属性。

针对您之前的评论,您可能会让一个活动实现onClick,并在xml文件中指定指向onClick。一般来说,我发现最好避免让您的片段/活动实现单击侦听器,因为当有多个按钮时会引起混乱。

此外,为了避免在addTaskNow中出现NullPointerException,您需要更改一些内容。您正在调用v.findViewById(R.id.editText1),这将返回null。这是因为findViewById只能返回包含在该视图中的视图。您传递到addTaskNow中的视图将是按钮,而不是包含R.id.editText1的根视图。

为了解决这个问题,您最好的选择是在片段中创建一个 EditText 成员变量,并在 onCreateView 中使用 R.id.editText1 进行初始化,就像您处理按钮一样。因此,您不再需要将 View 作为 addTaskNow 的参数。

谢谢!那个问题已经解决了,但现在我在addTaskNow方法中遇到了一个空指针异常错误。具体来说是在String s = t.getText().toString();这一行。 - Adilp
哦,那是因为你的findViewById(R.id.editText1)返回了null。我会更新答案。 - Trevor Siemens

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接