有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法添加自定义视图类

我希望能够使用layout_名称将自定义视图类添加到特定布局。添加视图(自定义视图类实例),或将它们添加到列表视图中

我打算以编程方式添加其中许多视图,并希望能够分别获得每个视图的编辑文本/微调器等值

当我尝试添加此自定义视图时,不会显示任何内容,我在第二次添加视图时也会收到此错误

 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
            at 安卓.view.ViewGroup.addViewInner(ViewGroup.java:3577)
            at 安卓.view.ViewGroup.addView(ViewGroup.java:3430)
            at 安卓.view.ViewGroup.addView(ViewGroup.java:3375)
            at 安卓.view.ViewGroup.addView(ViewGroup.java:3351)
            at com.modup.fragment.CreateFragment.onClick(CreateFragment.java:192)
            at 安卓.view.View.performClick(View.java:4442)
            at 安卓.view.View$PerformClick.run(View.java:18473)
            at 安卓.os.Handler.handleCallback(Handler.java:733)
            at 安卓.os.Handler.dispatchMessage(Handler.java:95)
            at 安卓.os.Looper.loop(Looper.java:136)
            at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
            at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:608)
            at dalvik.system.NativeStart.main(Native Method)

如何将此具有特定布局的WorkoutView添加到LinearLayout中,该LinearLayout是为setContentView()使用的视图而膨胀的原始布局的一部分

更新:

我正在尝试将此视图插入到一个布局中,该布局包含在一个父布局中,该父布局为将视图设置为setContentView()而膨胀

R.id.create_fragment_布局(在原始setContentView()中使用)

R.id.workup\u布局(在创建片段布局内)

I want to insert custom view into this layout, and this custom view needs to have its own R.id.somename_layout with many elements inside.

WorkoutView(自定义类)

public class WorkoutView extends LinearLayout implements View.OnClickListener {
    int res_id;
    public Spinner spinnerDifficulty, spinnerTime, spinnerMuscleGroup, spinnerSets, spinnerReps;
    public EditText etWorkoutName;
    public Button btnRemoveWorkout;

    String workoutName, muscleGroup, sets, reps;
    LayoutInflater inflater;

    public WorkoutView(Context context) {
        super(context);
    }

    public WorkoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WorkoutView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init() {
        inflate(getContext(), R.layout.single_workout_layout, this);

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

    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:
                Log.e("DID THIS WORK?", "BUTTON PRESSED");
                break;
        }

    }
}

我如何创建视图

workoutView = new WorkoutView(getActivity());

如何将视图添加到线性布局中

workoutLayout.addView(workoutView);

共 (2) 个答案

  1. # 2 楼答案

    您正在自定义类中扩展布局文件。因此,您已经在该自定义类中添加了一个视图,并且再次在这里执行相同的操作

    workoutLayout.addView(workoutView);
    

    所以问题就出现了。你需要改变以下内容

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        workoutView = new WorkoutView(getActivity());
        setContentView(workoutView);