无法访问自定义View类中的方法

时间:2015-03-07 02:57:05

标签: java android android-layout

我似乎无法访问我在自定义视图类WorkoutView中实现的任何方法。

我试图以编程方式将其中几个视图添加到父布局中的LinearLayout,该布局使用setContentView设置;

为什么我不能访问WorkoutView类中定义的任何方法?我需要访问这些方法,以便我可以获得这些每个视图的值(因为可以有添加了无限数量的这些视图)

以下是我想要使用WorkoutView

来扩展布局的方法
    inflater = getActivity().getLayoutInflater();
    workoutView = (WorkoutView) inflater.inflate(R.layout.single_workout_layout, workoutLayout, false);

这是最初在膨胀的父视图中的LinearLayout

   workoutLayout = (ViewGroup) view.findViewById(R.id.workoutLayout);

只需将视图添加到workoutLayout

即可
workoutLayout.addView(workoutView);

以下是父布局的XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context="com.modup.fragment.CreateFragment">

    <!-- TODO: Update blank fragment layout -->


    <!--    <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/scrollView" android:layout_gravity="center">-->

    <com.modup.view.CustomScrollView
            android:layout_width="match_parent"
            android:id="@+id/scrollView"
            android:layout_height="match_parent">


        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/scrollViewLinearLayout">


<!--            <android.support.v7.widget.CardView
                    xmlns:card_view="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/card_view"
                    android:layout_gravity="center"
                    card_view:cardElevation="0dp"
                    android:background="@android:color/white"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    >-->

                <LinearLayout
                        android:orientation="vertical"
                        android:background="@drawable/white_background_shadow"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent">


                    <LinearLayout
                            android:orientation="horizontal"
                            android:layout_margin="10dp"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent">

                        <ImageView
                                android:id="@+id/imageViewTitle"
                                android:layout_width="35dp"
                                android:layout_gravity="center"
                                android:src="@drawable/title"
                                android:layout_height="35dp"
                                />


                        <com.wrapp.floatlabelededittext.FloatLabeledEditText
                                xmlns:float="http://schemas.android.com/apk/res-auto"
                                android:layout_width="fill_parent"
                                android:layout_marginRight="10dp"
                                android:layout_marginLeft="10dp"
                                android:layout_gravity="center"
                                android:layout_height="wrap_content"
                                float:fletPaddingLeft="4dp"
                                float:fletPaddingBottom="2dp">

                            <EditText
                                    android:layout_width="fill_parent"
                                    android:maxLength="75"
                                    android:maxLines="3"
                                    android:layout_gravity="center"
                                    android:layout_height="wrap_content"
                                    android:inputType="textMultiLine"
                                    android:ems="10"
                                    android:hint="Title"
                                    android:id="@+id/editText"/>


                        </com.wrapp.floatlabelededittext.FloatLabeledEditText>
                    </LinearLayout>

                    <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="match_parent"
                            android:layout_height="1dp" android:layout_gravity="center_horizontal"
                            android:background="@color/material_grey_500" android:layout_margin="10dp"
                            android:id="@+id/divider" android:focusableInTouchMode="true">
                    </LinearLayout>

                    <LinearLayout
                            android:orientation="horizontal"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent">
                        <LinearLayout
                                android:orientation="horizontal"
                                android:layout_margin="10dp"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content" android:layout_weight="1">
                            <ImageView
                                    android:layout_width="36dp"
                                    android:src="@drawable/skull"
                                    android:layout_height="36dp"
                                    android:id="@+id/imageViewDifficulty" android:layout_gravity="center"
                                    android:adjustViewBounds="false"/>
                            <Spinner
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:id="@+id/spinnerDifficulty" android:layout_gravity="center"/>
                        </LinearLayout>
                        <LinearLayout
                                android:orientation="horizontal"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_margin="10dp">
                            <ImageView
                                    android:layout_width="35dp"
                                    android:layout_height="35dp"
                                    android:id="@+id/imageViewTime" android:src="@drawable/stopwatch"
                                    android:layout_gravity="center"/>
                            <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content"
                                     android:id="@+id/spinnerTime" android:layout_gravity="center"/>
                        </LinearLayout>
                    </LinearLayout>
                </LinearLayout>
<!--            </android.support.v7.widget.CardView>-->
            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" android:layout_margin="10dp"
                    >

                <Button
                        android:layout_width="45dp"
                        android:layout_height="45dp"
                        android:background="@drawable/add_content_selector"
                        android:id="@+id/buttonAddWorkout"/>

                <com.modup.font.RobotoTextView
                        android:id="@+id/addWorkout"
                        style="@style/TextViewAppearance.Title1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:text="Add Workout"
                        android:textSize="20sp"
                        android:textColor="@color/primary_blue"
                        app:typeface="robotoBold" android:layout_weight="1"
                        android:gravity="center" android:layout_gravity="center" android:adjustViewBounds="false"
                        android:layout_marginRight="45dp"/>
            </LinearLayout>
            <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:id="@+id/workoutLayout">
            </LinearLayout>


        </LinearLayout>
    </com.modup.view.CustomScrollView>
    <Button
            android:layout_width="65dp"
            android:isScrollContainer="false"
            android:layout_height="65dp"
            android:layout_margin="25dp"
            android:background="@drawable/accept_content_selector"
            android:id="@+id/buttonAccept" android:layout_gravity="right|bottom"/>
    <Button
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:isScrollContainer="false"
            android:layout_marginBottom="25dp"
            android:layout_marginRight="100dp"
            android:background="@drawable/cancel_content_selector"
            android:id="@+id/buttonCancel" android:layout_gravity="right|bottom"/>
</FrameLayout>

这是WorkoutView的XML

<?xml version="1.0" encoding="utf-8"?>
<com.modup.view.WorkoutView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:id="@+id/workoutView">


    <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="4dp" android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" android:layout_marginTop="2dp" android:layout_marginBottom="2dp">

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                <EditText
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:hint="Enter Workout Name"
                        android:id="@+id/etWorkoutName" android:layout_margin="5dp" android:layout_weight="1"/>
                <Button
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:background="@drawable/cancel_content_selector"
                        android:id="@+id/btnRemoveWorkout" android:layout_margin="5dp"/>
            </LinearLayout>
            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:layout_margin="5dp">
                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" android:layout_weight="1">
                    <com.modup.font.RobotoTextView
                            android:id="@+id/textViewMuscleGroup"
                            style="@style/TextViewAppearance.Title1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/primary_blue"
                            android:text="Muscle Group"
                            android:textSize="15sp"
                            app:typeface="robotoBold"
                            android:orientation="horizontal"
                            android:gravity="center" android:padding="5dp"/>
                    <Spinner
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:id="@+id/spinnerWorkoutGroup" android:textAlignment="center"
                            android:layout_weight="1"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:layout_margin="5dp">
                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" android:layout_weight="1">
                    <com.modup.font.RobotoTextView
                            android:id="@+id/textViewSet"
                            style="@style/TextViewAppearance.Title1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/primary_blue"
                            android:text="Sets"
                            android:textSize="15sp"
                            app:typeface="robotoBold"
                            android:orientation="horizontal"
                            android:gravity="center" android:padding="5dp"/>
                    <Spinner
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/spinnerSets" android:textAlignment="center" android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" android:layout_weight="1">
                    <com.modup.font.RobotoTextView
                            android:id="@+id/textViewRep"
                            style="@style/TextViewAppearance.Title1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/primary_blue"
                            android:text="Reps"
                            android:textSize="15sp"
                            app:typeface="robotoBold"
                            android:orientation="horizontal"
                            android:gravity="center" android:padding="5dp"/>
                    <Spinner
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/spinnerReps" android:textAlignment="center" android:layout_weight="1"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</com.modup.view.WorkoutView>

这是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);
    init();
    }

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

    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) this.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中的方法,例如

workoutView.getReps();

导致NullPointerException

java.lang.NullPointerException
        at com.modup.view.WorkoutView.getReps(WorkoutView.java:51)
        at com.modup.fragment.CreateFragment.onClick(CreateFragment.java:197)

更新

我添加了init();构造函数的方法导致此错误

03-06 22:48:06.952  22106-22106/com.modup.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.modup.app, PID: 22106
android.view.InflateException: Binary XML file line #2: Error inflating class com.modup.view.WorkoutView
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at com.modup.fragment.CreateFragment.onClick(CreateFragment.java:195)
        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)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.modup.fragment.CreateFragment.onClick(CreateFragment.java:195)
            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)
 Caused by: java.lang.NullPointerException
        at com.modup.view.WorkoutView.init(WorkoutView.java:57)
        at com.modup.view.WorkoutView.<init>(WorkoutView.java:39)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.modup.fragment.CreateFragment.onClick(CreateFragment.java:195)
            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)

1 个答案:

答案 0 :(得分:2)

  

我无法调用WorkoutView中的方法...导致NullPointerException

这是因为您期望由findViewById()设置的子视图尚未生效。在第一种情况下,它是尝试访问getSelectedItem()

  

我添加了init();构造函数的方法导致此错误

同样的原因,在这种情况下,它是setOnClickListener()末尾对init()的号召。

在这两种情况下,您都假设在调用构造函数时,XML文件中定义的视图层次结构已经完全膨胀...这是错误的。调用构造函数时,所有子视图都未被充气并添加。

如果您希望以编写init()方法的方式引用子视图,则必须等到onFinishInflate()方法被触发(docs link)。重写此方法并在那里调用init() ...不在构造函数中:

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

然后,您尝试调用访问者方法应该按预期工作。