从其自己的视图类中删除视图

时间:2015-03-07 04:35:15

标签: android view interface

这是我创建的视图类。

public class WorkoutView extends LinearLayout implements View.OnClickListener {


    public Spinner spinnerDifficulty, spinnerTime, spinnerMuscleGroup, spinnerSets, spinnerReps;
    public EditText etWorkoutName;
    public Button btnRemoveWorkout;

    String workoutName, muscleGroup, sets, reps;
    LayoutInflater inflater;
    Context mContext;
    ButtonPressInterface buttonPressCallback;

    public WorkoutView(Context context) {
        super(context);
        Log.e("WORKOUTVIEW", "1");
        mContext = context;

    }

    public WorkoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        Log.e("WORKOUTVIEW", "2");
    }

    public WorkoutView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        Log.e("WORKOUTVIEW", "3");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    public interface ButtonPressInterface {
        void buttonWasPressed();

    }

    public void init() {
        spinnerDifficulty = (Spinner) findViewById(R.id.spinnerDifficulty);
        spinnerReps = (Spinner) findViewById(R.id.spinnerReps);
        spinnerSets = (Spinner) findViewById(R.id.spinnerSets);
        spinnerMuscleGroup = (Spinner) findViewById(R.id.spinnerWorkoutGroup);
        spinnerTime = (Spinner) findViewById(R.id.spinnerTime);
        etWorkoutName = (EditText) findViewById(R.id.etWorkoutName);
        btnRemoveWorkout = (Button) findViewById(R.id.btnRemoveWorkout);
        btnRemoveWorkout.setOnClickListener(this);

        buttonPressCallback = (ButtonPressInterface) getContext();
    }

    public String getReps() {
        reps = spinnerReps.getSelectedItem().toString();
        return reps;
    }

    public String getWorkoutName() {
        workoutName = etWorkoutName.getText().toString().trim();
        return workoutName;
    }

    public String getMuscleGroup() {
        muscleGroup = spinnerMuscleGroup.getSelectedItem().toString();
        return muscleGroup;
    }

    public String getSets() {
        sets = spinnerSets.getSelectedItem().toString();
        return sets;
    }

    public SingleWorkout getAll() {
        //to be designed
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnRemoveWorkout:
                buttonPressCallback.buttonWasPressed();
                Log.e("DID THIS WORK?", "BUTTON PRESSED");
                break;
        }

    }
}

如何在按btnRemoveWorkout时删除此视图?有没有办法实现接口?正在正确添加此视图,所有其他方法都可以正常工作。

以下是我尝试以编程方式添加视图时收到的错误,在尝试实现回调活动的接口之前,这是可能的。

03-06 23:30:30.352    6986-6986/com.modup.app E/WORKOUTVIEW﹕ 2
03-06 23:30:30.372    6986-6986/com.modup.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.modup.app, PID: 6986
    java.lang.ClassCastException: com.modup.view.WorkoutView cannot be cast to com.modup.view.WorkoutView$ButtonPressInterface
            at com.modup.view.WorkoutView.init(WorkoutView.java:70)
            at com.modup.view.WorkoutView.onFinishInflate(WorkoutView.java:52)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:763)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.modup.fragment.CreateFragment.onClick(CreateFragment.java:197)
            at android.view.View.performClick(View.java:4442)
            at android.view.View$PerformClick.run(View.java:18473)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5105)
            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:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
            at dalvik.system.NativeStart.main(Native Method)
03-06 23:31:21.972    7620-7620/com.modup.app E/WORKOUTVIEW﹕ 2
03-06 23:31:21.982    7620-7620/com.modup.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.modup.app, PID: 7620
    java.lang.ClassCastException: com.modup.app.LeftMenusActivity cannot be cast to com.modup.view.WorkoutView$ButtonPressInterface
            at com.modup.view.WorkoutView.init(WorkoutView.java:70)
            at com.modup.view.WorkoutView.onFinishInflate(WorkoutView.java:52)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:763)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.modup.fragment.CreateFragment.onClick(CreateFragment.java:197)
            at android.view.View.performClick(View.java:4442)
            at android.view.View$PerformClick.run(View.java:18473)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5105)
            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:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
            at dalvik.system.NativeStart.main(Native Method)

我期待回应回调的片段内部

@Override
public void buttonWasPressed() {
     workoutLayout.removeView(workoutView);
}

更新

我实施了

   public void registerButtonPressInterface(ButtonPressInterface listener){
        buttonPressCallback = listener;
    }

添加视图时不会导致崩溃,但我的按钮仍无效,导致NullPointerException。

    @Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btnRemoveWorkout:
            buttonPressCallback.buttonWasPressed();
            Log.e("DID THIS WORK?", "BUTTON PRESSED");
            break;
    }

}

这就是我试图收听回调的方式

        workoutView.registerButtonPressInterface(new WorkoutView.ButtonPressInterface() {
        @Override
        public void buttonWasPressed() {
            Log.e("BUTTON WAS PRESSED", "INTERFACE WORKED!");
        }
    });
    workoutLayout.addView(workoutView);

1 个答案:

答案 0 :(得分:1)

根据您的堆栈跟踪,这是导致胃灼热的一行:

buttonPressCallback = (ButtonPressInterface) getContext();

您没有发布相关代码,但我敢打赌您认为您的活动实际上并非如此。实际上,你说你的最后一个代码片段(它是接口实现)来自片段。

以下是我的建议:不要将上下文强制转换为界面,而是创建方法

public void registerButtonPressListener(ButtonPressInterface listener)

在您的视图类上

并存储对ButtonPressInterface实现的引用。这将使您的视图类更加灵活,因为您的上下文不需要实现ButtonPressInterface才能使用您的视图。现在,编译器将能够告诉您传入的内容实际上是否实现了接口,并且您将更容易找出错误。