RecyclerView“无结果”TextView findViewById始终返回null

时间:2015-12-16 23:22:10

标签: android nullpointerexception textview android-recyclerview

我有一个RecyclerView,其中包含TextView,以及没有找到结果时。

调用TextView时此root.findViewById始终返回null,我尝试重新清理项目,重新构建项目。没有任何帮助。

以下是我的导入:

     import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.HandlerThread;
    import android.os.Message;
    import android.support.v4.app.Fragment;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.text.TextUtils;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ProgressBar;
    import android.widget.TextView;

    import com.mypackage.commoncode.R;

以下是onCreateView的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_mail, container, false);
    //
    mProgressBar = (ProgressBar) root.findViewById(R.id.spinner);
    mProgressBar.setVisibility(View.VISIBLE);

    final Context context = root.getContext();

    mEmptyView = (TextView) root.findViewById(R.id.empty_view);
    mEmptyView.setVisibility(View.INVISIBLE);

    // set list
    float ht = BitmapUtils.convertDpToPixel(8, context);
    mLinearLayoutManager = new LinearLayoutManager(context);
    mListView = (RecyclerView) root.findViewById(android.R.id.list);
    mListView.setLayoutManager(mLinearLayoutManager);
    mListView.setVisibility(View.INVISIBLE);
    mListView.addItemDecoration(new VerticalSpaceItemDecoration(Float.valueOf(ht).intValue()));

    mListAdapter = new InboxListAdapter(context);
    mListAdapter.setOnInboxClickListener(this);
    mListView.setAdapter(mListAdapter);

    if (mBackground == null) {
        mBackground = new HandlerThread("background", android.os.Process.THREAD_PRIORITY_BACKGROUND);
        mBackground.start();
        mBackgroundCallback = new BackgroundCallback(this.getActivity().getApplicationContext(), mCurrentKid);
        mBackgroundHandler = new Handler(mBackground.getLooper(), mBackgroundCallback);
    }

    final Handler displayHandler = new Handler(new HandlerCallback(this));
    mBackgroundCallback.setDisplayHandler(displayHandler);
    //
    retrieveInboxList(context);

    return root;
}

以下是布局的XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ProgressBar
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center|center_horizontal" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TextView
    android:id="@+id/empty_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:visibility="invisible"
    android:text="@string/No_Results_found" />
</FrameLayout>

关于empty_view TextView为何始终为空的任何想法?

Logcat错误:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference 

在这一行:

mEmptyView.setVisibility(View.INVISIBLE);

1 个答案:

答案 0 :(得分:1)

引用的视图xml没有包含您尝试检索的ID的视图。确保发布的xml是正确的,或者您有另一个xml用于此分辨率可能不包含该视图。