Android - setOnClickListener在片段中崩溃

时间:2014-12-01 20:45:34

标签: android android-fragments android-imagebutton

我正在开展一项使用两个片段的活动,一个用于在画布上显示图像,另一个用于影响画布上元素的按钮。我刚刚为这两个按钮设置了onClickListeners,现在程序正在崩溃,之前它没有崩溃。这种情况在运行时会立即发生,所以我不知道究竟是什么问题,除了logcat说它完全是这个片段的问题。我只能假设我没有错误地实现setOnClickListener。谁能帮我看看我做错了什么?

ToolbarFragment

package com.example.chris.drawingtest;

import android.app.Activity;
import android.app.Fragment;
import android.content.DialogInterface;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

import com.example.chris.drawingtest.R;

/**
 * Created by Chris on 11/28/2014.
 */
public class ToolbarFragment extends Fragment {

    ToolSelectionListener mCallback;

    ImageButton pencilButton, eraserButton;

    public interface ToolSelectionListener {
        public void sendNewValue(int newValue);
    }

    public void clicked(ImageButton imageButton) {
        Log.d("we're listening...", "to the buttons!");
        if (imageButton.getId() == eraserButton.getId())
            mCallback.sendNewValue(1);
        else
            mCallback.sendNewValue(0);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        pencilButton = (ImageButton) getActivity().findViewById(R.id.pencil);
        eraserButton = (ImageButton) getActivity().findViewById(R.id.eraser);

        pencilButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                clicked(pencilButton);
            }
        });

        eraserButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                clicked(eraserButton);
            }
        });

        Log.d("Toolbar Inflation", "Inflating ToolbarFragment...");
        return inflater.inflate(R.layout.fragment_toolbar, container, false);

    }
}

**编辑:**这里也是匹配的XML文件。

fragment_toolbar:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_gravity="bottom|left"
    android:orientation="horizontal"
    >

    <ImageButton
        android:id="@+id/pencil"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="left"
        android:background="@drawable/pencil"
        />

    <ImageButton
        android:id="@+id/eraser"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:background="@drawable/eraser"
        />

</LinearLayout>

感谢您提供任何帮助!

3 个答案:

答案 0 :(得分:3)

你的部分代码的问题是在Fragment中你必须给视图充气。只有在退货时才会给它充气。尝试以这种方式更改onCreateView

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_toolbar, container, false);
        pencilButton = (ImageButton) view.findViewById(R.id.pencil);
        eraserButton = (ImageButton) view.findViewById(R.id.eraser);

        pencilButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                clicked(pencilButton);
            }
        });

        eraserButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                clicked(eraserButton);
            }
        });

        Log.d("Toolbar Inflation", "Inflating ToolbarFragment...");
        return view;

    }

答案 1 :(得分:1)

您之所以获得例外,是因为您在ImageButtons之前尝试调用其功能,然后再进行虚增。在setOnCallListener之后看看你的inflater是如何膨胀的?

onCreateView中,您通过调用pencilButton关闭getActivity来不正确地引用按钮,eraserButtonfindViewById()

相反,将该逻辑移植到onViewCreated并在findViewById(R.id.xxx)参数上调用View

答案 2 :(得分:1)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_toolbar, container, false);

    pencilButton = (ImageButton) view.findViewById(R.id.pencil);
    eraserButton = (ImageButton) view.findViewById(R.id.eraser);

    pencilButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            clicked(pencilButton);
        }
    });

    eraserButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            clicked(eraserButton);
        }
    });

    Log.d("Toolbar Inflation", "Inflating ToolbarFragment...");
    return view;

}