Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object referen

本文深入探讨了在使用RecyclerView时遇到的空布局参数异常问题,详细解析了NullPointerException的产生原因,即尝试从null对象引用读取字段。通过对比LayoutInflater.inflate方法的不同调用方式,提供了解决方案,强调了正确传递父视图的重要性。
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: kodulf.swiperefreshlayoutrecyclerviewdemo, PID: 8661
    java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
        at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:7547)
        at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7861)
        at kodulf.swiperefreshlayoutrecyclerviewdemo.MainActivity$MyAdapter.onCreateViewHolder(MainActivity.java:181)
        at kodulf.swiperefreshlayoutrecyclerviewdemo.MainActivity$MyAdapter.onCreateViewHolder(MainActivity.java:161)
        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)

 

因为inflate的时候:

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, null, false);


解决办法:
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
Android Java 代码中对 ImageButton 设置宽高时出现 `'Attempt to invoke virtual method 'void android.widget.ImageButton.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference'` 错误,通常是因为没有正确获取 ImageButton 的实例,也就是 `findViewById` 方法没有正确找到对应的 ImageButton。 以下是一些可能的解决办法: ### 检查布局文件和 ID 确保布局文件中 ImageButton 的 ID 与 Java 代码中 `findViewById` 方法使用的 ID 一致。例如,布局文件如下: ```xml <com.qish.tenmentapp.config.WrapLinearLayout android:id="@+id/list_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/size30" app:layout_constraintTop_toBottomOf="@id/title" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <ImageButton android:id="@+id/btn_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_take_photo" android:background="@color/black" /> </com.qish.tenmentapp.config.WrapLinearLayout> ``` Java 代码中获取 ImageButton 实例时,ID 要一致: ```java import android.os.Bundle; import android.widget.ImageButton; import android.widget.LinearLayout; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 ImageButton 实例 ImageButton imageButton = findViewById(R.id.btn_photo); if (imageButton != null) { // 创建 LinearLayout.LayoutParams 对象 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, // 宽度 LinearLayout.LayoutParams.WRAP_CONTENT // 高度 ); // 设置具体的宽高值,这里以 200dp 为例 int widthInDp = 200; int heightInDp = 200; // 将 dp 转换为 px float density = getResources().getDisplayMetrics().density; int widthInPx = (int) (widthInDp * density); int heightInPx = (int) (heightInDp * density); // 设置宽高 layoutParams.width = widthInPx; layoutParams.height = heightInPx; // 应用布局参数 imageButton.setLayoutParams(layoutParams); } } } ``` ### 检查 setContentView 方法 确保 `setContentView` 方法中传入的布局文件是包含 ImageButton 的正确布局文件。如果传入的布局文件不正确,`findViewById` 就无法找到 ImageButton,从而返回 `null`。 ### 检查布局文件是否加载完成 确保在布局文件加载完成后再进行 ImageButton 宽高的设置。一般来说,在 `onCreate` 方法中调用 `setContentView` 后,布局文件会立即加载,所以通常不会有这个问题。但如果是在异步操作中进行 `findViewById`,可能会出现布局文件还未加载完成的情况。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值