atempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null obje

今天在做开发的时候,碰到这个错误,搞了半天不知道啥意思,因为从下面的代码看,没有一点问题,最后没办法,只能采用最笨的办法,排除法。

  @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ConString.VIEW_TYPE_TITLE) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_elev_detail_title, parent, false);
            return new TitleViewHolder(view);
        } else if (viewType == ConString.VIEW_TYPE_CONTENT) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.item_elev_detail_content, parent, false);
            return new ContentViewHolder(rootView);
        }
        return null;
    }

步骤如下
1 直接把多布局删除,使用下面的content布局,结果正常进入了页面,说明这个布局没有问题。

2 直接使用上面的title布局,结果还是挂了,说明错误与布局有关,下面看了看布局,非常简单的,里面就一个textView,还有一个间隔线。找来找去都找不到错误。没办法,直接把布局删除,重新写。结果这次没有崩溃。再仔细对比原来的布局,发现犯了一个小白都不会犯的错误view 写错了,应该是View。记录此次错误,以作反思。

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:orientation="vertical">

    <view
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/color_text_f2f3f4" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="@dimen/margin10"
        android:layout_marginBottom="10dp"
        android:text=""
        android:textColor="@color/color_text_404852"
        android:textSize="14sp" />


</LinearLayout>

总结:编代码 的时候需要严谨,一点小错都不能犯,否则出了错误,像这种空指针,只是把你引到沟里去,浪费时间

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章