gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:54:38 33 4
gpt4 key购买 nike

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

我正在尝试以编程方式将其中几个 View 添加到使用 setContentView 设置的父布局中的 LinearLayout;

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

这是我如何扩展我想与 WorkoutView 一起使用的布局

    inflater = getActivity().getLayoutInflater();
workoutView = (WorkoutView) inflater.inflate(R.layout.single_workout_layout, workoutLayout, false);

这是最初膨胀的父 View 内的 LinearLayout

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

只需将 View 添加到锻炼布局

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)

最佳答案

I cannot make a call to the methods within the WorkoutView…it results in a NullPointerException

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

I added the init(); method to the constructor and it results in this error

同样的原因,在本例中是在 init() 末尾调用 setOnClickListener()

在这两种情况下,您都假设 XML 文件中定义的 View 层次结构在调用构造函数时已完全膨胀……这是错误的。当构造函数被调用时,下面的所有 subview 还没有被膨胀和添加。

如果您想以编写 init() 方法的方式获取对 subview 的引用,则必须等到 onFinishInflate() 方法被调用触发(docs link)。重写此方法并在那里调用 init()...而不是在构造函数中:

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

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

关于java - 无法访问自定义 View 类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28910883/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com