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

Removing a view from within its own view class

这是我创建的 class 视图。

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 时删除此视图?有实现接口的方法吗? 正确添加了这个视图,所有其他方法都运行良好。

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

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);

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

buttonPressCallback = (ButtonPressInterface) getContext();

您没有发布相关代码,但我敢打赌您认为您的 activity 正在实现此接口,但实际上并没有。实际上,您说您的最后一个代码片段(即接口实现)来自 Fragment。

这是我的建议:与其将上下文强制转换为您的界面,不如创建一个方法

public void registerButtonPressListener(ButtonPressInterface listener)

在您的视图中 class 并存储对 ButtonPressInterface 实现的引用。这将使您的视图 class 更加灵活,因为您的上下文不需要实现 ButtonPressInterface 即可使用您的视图。现在编译器将能够告诉您您传入的内容是否确实实现了接口,并且您将更容易找出问题所在。