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>

總結:編代碼 的時候需要嚴謹,一點小錯都不能犯,否則出了錯誤,像這種空指針,只是把你引到溝裏去,浪費時間

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