java.lang.NullPointerException:尝试在Fragement中调用虚方法错误

时间:2016-07-07 13:46:58

标签: java android

我试图在片段onCreateVIew()方法中传递一个上下文,但是我收到以下错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

这是我的代码

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.zoggfrombetelgeuse.closet.MySQL.mDownloader;



public class HomeFragment extends Fragment {

public HomeFragment() {
    // Required empty public constructor
}



static String urlAddress = "http://localhost/my-site/dbGet.php";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    final RecyclerView rv = (RecyclerView) view.findViewById(R.id.card_view);
    rv.setLayoutManager(new LinearLayoutManager(getActivity()));

    final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_layout);

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            new mDownloader(HomeFragment.this.getActivity(), urlAddress, rv, swipeRefreshLayout).execute();
        }
    });

    return view;

}

}

有人请用简单的语言解释这个错误,并帮我修复它。我已经阅读了其他问题,我使用onAttach()但没有工作

HomeFragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zoggfrombetelgeuse.closet.HomeFragment">

<!-- TODO: Update blank fragment layout -->
<android.support.v4.widget.SwipeRefreshLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/swipe_layout">
<android.support.v7.widget.RecyclerView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/recycler_view"
    android:scrollbars="vertical">
    </android.support.v7.widget.RecyclerView>

</android.support.v4.widget.SwipeRefreshLayout>



</FrameLayout>

0 个答案:

没有答案