尝试使用RecyclerView

时间:2017-07-09 22:52:27

标签: java android

我试图在片段中使用recyclelerView,但每当我运行应用程序时,我在视图上调用setLayoutManager时会得到一个nullpointer异常。我跟着android文件使用了recyclelerview和片段,并在这里查看了与此问题相关的帖子,但似乎没有任何效果。

  

`07-09 17:40:27.784   4565-4565 / com.example.broulayedoumbia.restaurantapp E / AndroidRuntime:   致命异议:主要                                                                                            过程:com.example.broulayedoumbia.restaurantapp,PID:4565                                                                                            java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.broulayedoumbia.restaurantapp / com.example.broulayedoumbia.restaurantapp.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法' void   android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $布局管理)'   在null对象引用上                                                                                                在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)                                                                                                在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)                                                                                                在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                                在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)                                                                                                在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                                在android.os.Looper.loop(Looper.java:154)                                                                                                在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                                at java.lang.reflect.Method.invoke(Native Method)                                                                                                在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                                在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)                                                                                             引起:java.lang.NullPointerException:尝试调用虚拟   方法'无效   android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $布局管理)'   在null对象引用上                                                                                                在   com.example.broulayedoumbia.restaurantapp.RestaurantListFragment.onCreateView(RestaurantListFragment.java:79)                                                                                                在   android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)

`

片段代码:

public class RestaurantListFragment extends Fragment implements RestaurantAdapter.ListItemClickListener{

private static final int NUM_LIST_ITEMS = 100;



private OnFragmentInteractionListener mListener;

private RestaurantAdapter mAdapter;
private RecyclerView mRestaurantList;

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

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment RestaurantListFragment.
 */
// TODO: Rename and change types and number of parameters
public static RestaurantListFragment newInstance(String param1, String param2) {
    RestaurantListFragment fragment = new RestaurantListFragment();
    Bundle args = new Bundle();

    fragment.setArguments(args);
    return fragment;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_login, container, false);

    /*
     * Using findViewById, we get a reference to our RecyclerView from xml. This allows us to
     * do things like set the adapter of the RecyclerView and toggle the visibility.
     */
    mRestaurantList = (RecyclerView) view.findViewById(R.id.rest_list);


    /*
     * A LinearLayoutManager is responsible for measuring and positioning item views within a
     * RecyclerView into a linear list. This means that it can produce either a horizontal or
     * vertical list depending on which parameter you pass in to the LinearLayoutManager
     * constructor. By default, if you don't specify an orientation, you get a vertical list.
     * In our case, we want a vertical list, so we don't need to pass in an orientation flag to
     * the LinearLayoutManager constructor.
     *
     * There are other LayoutManagers available to display your data in uniform grids,
     * staggered grids, and more! See the developer documentation for more details.
     */
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());

    mRestaurantList.setLayoutManager(layoutManager);

    /*
     * Use this setting to improve performance if you know that changes in content do not
     * change the child layout size in the RecyclerView
     */
    mRestaurantList.setHasFixedSize(true);

    mAdapter = new RestaurantAdapter(NUM_LIST_ITEMS, this);
    mRestaurantList.setAdapter(mAdapter);

    return view;
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

@Override
public void onListItemClick(int clickedItemIndex) {

}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {

}
}

片段Xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

主要活动:

public class MainActivity extends AppCompatActivity implements LoginFragment.OnFragmentInteractionListener, RestaurantListFragment.OnFragmentInteractionListener {

private LoginFragment loginFragment;
private RestaurantListFragment restaurantListFragment;
private FragmentTransaction fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fragmentManager = getSupportFragmentManager().beginTransaction();

    loginFragment = new LoginFragment();
    restaurantListFragment = new RestaurantListFragment();

    fragmentManager.replace(R.id.fragContainer, restaurantListFragment).commit();

}

@Override
public void login() {

}
}

0 个答案:

没有答案