- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 init 方法中,findViewById 在 R.id.disable_view_content 上返回 null。
public class DisableView extends RelativeLayout {
private View content;
private View disableView;
private int disableBackgroundColor;
private boolean isEnabled;
public DisableView(Context context) {
super(context);
init();
}
public DisableView(Context context, AttributeSet attrs) {
super(context, attrs);
obtainAttributes(context, attrs);
init();
}
public DisableView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
obtainAttributes(context, attrs);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public DisableView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
obtainAttributes(context, attrs);
init();
}
private void obtainAttributes(Context context, AttributeSet attributeSet) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.DisableView, 0, 0);
disableBackgroundColor = typedArray.getColor(R.styleable.DisableView_disableBackground, 0x00000000);
isEnabled = typedArray.getBoolean(R.styleable.DisableView_enabled, false);
typedArray.recycle();
}
private void init() {
content = findViewById(R.id.disable_view_content);
disableView = new View(getContext());
RelativeLayout.LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.ALIGN_TOP, R.id.disable_view_content);
lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.disable_view_content);
lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.disable_view_content);
lp.addRule(RelativeLayout.ALIGN_TOP, R.id.disable_view_content);
disableView.setBackgroundResource(disableBackgroundColor);
disableView.setLayoutParams(lp);
setEnabled(isEnabled);
}
public void setEnabled(boolean enabled) {
content.setEnabled(enabled);
if(enabled) disableView.setVisibility(View.GONE);
else disableView.setVisibility(View.VISIBLE);
}
}
这是xml
<com....view.common.DisableView
android:id="@+id/..._disableView_...Disable"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@id/disable_view_content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/..._textView_..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_rect_green"
android:gravity="center"
android:paddingTop="18dp"
android:paddingBottom="18dp"
android:layout_marginBottom="12dp"
android:textSize="@dimen/size_text"
android:text="@string/..."
android:textColor="@android:color/white"
android:duplicateParentState="true"/>
</LinearLayout>
</com....view.common.DisableView>
id disable_view_content 在 attr.xml 中
<resources>
<item type="id" name="disable_view_content"/>
</resources>
一切似乎都很好,id 为 disable_view_content 的 View 是 DisableView 的直接 subview 。但是无论是在编辑模式下还是在应用程序上,我都会得到一个空指针,因为引用内容是空的。
编辑:这是堆栈跟踪:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setEnabled(boolean)' on a null object reference
at com....view.common.DisableView.setEnabled(DisableView.java:71)
at com....view.common.DisableView.init(DisableView.java:67)
at com....view.common.DisableView.<init>(DisableView.java:35)
最佳答案
您在 View 的生命周期中过早地调用了 findViewById()
。
您的 init()
是直接从您的构造函数调用的,甚至在您为自己的 View 创建布局之前。此时,您的 View 没有布局,也没有 subview 。
最早您可以访问 subview 是在 addView()
之一之后方法已被调用。
关于android - FindViewById 在自定义相对布局上返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31883242/
我是 android 编程的新手,我对 View.findViewById() 和 Activity.findViewById() 很困惑,我们什么时候必须调用这些方法以及在哪个方法中调用类 find
/** * A placeholder fragment containing a simple view. */ public static class Placehol
我尝试在 java 中设置来自 TextView 的文本:首先我创建了一个这样的对象: //Property info TextView propertyInfo = (TextView) info.
[更新] -- 只是想向所有人确认,我同意使用硬编码号码总是一个坏主意。我的好奇心是在使用 Maps API 时枚举的 1、2 常量值是什么。如果我找到像 R.id.my_current_locati
在我的 fragment 的 onCreateView 方法中,我曾经将膨胀的布局存储在一个变量中,然后将后者传递给我的其他函数,这些函数在这个膨胀的 上调用了 findViewById >查看。 现
我正在尝试实现自定义数组适配器, 问题是我的代码在使用时崩溃 ImageView imageView=(ImageView)findViewById(R.id.imageView); 而不是 Ima
我想在多个 Activity 中使用 View 。就我而言,它是一个 FloatingActionButton。当我通过 findViewById 搜索 View 时,程序抛出 NullPointer
findViewById 给出一个错误,我认为这是因为它返回 null。我的应用程序被迫关闭。 SensorManager sensorManager=null; TextView x= null;
我刚刚创建了简单的应用程序并添加了按钮。 MyActivity.java package com.example.myapp; import android.app.Activity; import
我的应用程序在布局中有许多元素(远超过 100 个),并且在 MainActivity 中声明所有元素以便访问它们,这使得我的 MainActivity 类难以阅读和调试。因此,我创建了一个新类来处理
我刚刚开始android开发,我被困在这个: static Handler h = new Handler() { @Override public void handleMessag
我是一名初学者,正在磨练我的应用程序开发技能。我目前正在做一个示例,其中列表由字符串数组填充。在最后的步骤中进行绑定(bind),这是找不到方法“findviewbyid”的地方,并且在构建后显示为红
这是我函数的一部分,android studio 无法识别 findViewById 函数。谁能帮我找出问题所在吗?\ public void onClick(View v) {
我在 LinearLayout 内的相对布局内访问 TextView 时遇到问题 假设我在文件 row.xml 中有这些 View
很抱歉标题含糊不清, 下面是麻烦。我想使用 x 变量来迭代该对象,该对象在名称中使用 1 -9 值 (R.id.imageButtonx) for (int x = 1; x <10; x++)
谁能帮我找出这个程序的问题所在。在 onCreate() 方法中,findViewById() 为所有 id 返回 null,这会导致稍后出现空指针异常。我不明白为什么 findViewById()
我做错了什么,我检查了几次ID,在XML中一切正常,但是当我想构建发布应用程序时,会出现此错误,当我构建调试应用程序时,我不会出现此错误。 error: cannot find symbol
我是 Android 编程新手,所以我有一个简单的问题!我在 activity_main.xml 和 main_activity.java 中创建了一个 button 我想通过调用 findbyvie
我对 Android 开发非常陌生,所以如果我无法理解这里到底发生了什么,我很抱歉。 我创建了一个形状,如下所示。它包含在可绘制文件夹内的 shape.xml 中: 现在在我的 Acti
谁能帮我找出这个程序的问题所在。在 onCreate() 方法中,findViewById() 为所有 id 返回 null,这会导致稍后出现空指针异常。我不明白为什么 findViewById()
我是一名优秀的程序员,十分优秀!