RecyclerView:尝试对空对象引用调用虚拟方法

Dan*_*ena 4 android android-recyclerview

当我尝试测试我的应用程序时遇到问题,我是 Android Studio 的初学者并试图将 reyclerview 放在片段中,但是当我编译时,我收到此消息:

 Caused by: 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
Run Code Online (Sandbox Code Playgroud)

这是导致原因的文件:

package e.user.popcorn;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private BottomNavigationView mBottomNavigation;
private FrameLayout mMainFrame;

private HomeFragment homeFragment;
private MoviesFragment moviesFragment;
private UsersFragment usersFragment;
private ChatFragment chatFragment;
private SweetsFragment sweetsFragment;

List<Movie_data> bo_movies;

TextView mTopBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);

    mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
    mBottomNavigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);


    homeFragment = new HomeFragment();
    moviesFragment = new MoviesFragment();
    usersFragment = new UsersFragment();
    chatFragment = new ChatFragment();
    sweetsFragment = new SweetsFragment();

    bo_movies = new ArrayList<>();
    bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));
    bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));
    bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));
    bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));
    bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));

    RecyclerView myRv = (RecyclerView) findViewById(R.id.recyclerView_id);
    RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,bo_movies);
    myRv.setLayoutManager(new GridLayoutManager(this,3));
    myRv.setAdapter(myAdapter);

    setFragment(homeFragment);
    mTopBar = (TextView) findViewById(R.id.top_bar_title);
    mTopBar.setText(R.string.nav_item_home);

    mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {

                case R.id.nav_home :
                    setFragment(homeFragment);
                    mTopBar = (TextView) findViewById(R.id.top_bar_title);
                    mTopBar.setText(R.string.nav_item_home);
                    return true;

                case R.id.nav_movies :
                    setFragment(moviesFragment);
                    mTopBar = (TextView) findViewById(R.id.top_bar_title);
                    mTopBar.setText(R.string.nav_item_movies);
                    return true;

                case R.id.nav_users :
                    setFragment(usersFragment);
                    mTopBar = (TextView) findViewById(R.id.top_bar_title);
                    mTopBar.setText(R.string.nav_item_users);
                    return true;

                case R.id.nav_chat :
                    setFragment(chatFragment);
                    mTopBar = (TextView) findViewById(R.id.top_bar_title);
                    mTopBar.setText(R.string.nav_item_chat);
                    return true;

                case R.id.nav_sweets :
                    setFragment(sweetsFragment);
                    mTopBar = (TextView) findViewById(R.id.top_bar_title);
                    mTopBar.setText(R.string.nav_item_sweets);
                    return true;

                default :
                    return false;

            }


        }

    });

}


private void setFragment(Fragment fragment) {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame, fragment);
    fragmentTransaction.commit();

}
}
Run Code Online (Sandbox Code Playgroud)

这是 fragment_home.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".HomeFragment"
    android:background="@color/colorMainBackgroundDark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"></LinearLayout>

    <TextView
        android:id="@+id/title_latest_trailers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_latest_trailers"
        android:textSize="20sp"
        android:textAlignment="center"
        android:layout_margin="16dp"
        android:textColor="@color/colorWhite"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/video_frame"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:background="@color/colorWhite"
        android:contentDescription="TODO"
        android:layout_below="@+id/title_latest_trailers"/>

    <TextView
        android:id="@+id/title_box_office"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_latest_trailers"
        android:textSize="20sp"
        android:textAlignment="center"
        android:layout_margin="16dp"
        android:textColor="@color/colorWhite"
        android:textStyle="bold"
        android:layout_below="@+id/video_frame"/>

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

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是 HomeFragment.java

public class HomeFragment extends Fragment {

    Toolbar mTopBar;

    List<Movie_data> bo_movies;

    public HomeFragment() {


        bo_movies = new ArrayList<>();
        bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));
        bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));
        bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));
        bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));
        bo_movies.add(new Movie_data("Avengers", "Action", "Description de ouf", R.drawable.test));



        // Required empty public constructor

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        RecyclerView myRv = (RecyclerView) findViewById(R.id.recyclerView_id);
        RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,bo_movies);
        myRv.setLayoutManager(new GridLayoutManager(this,3));
        myRv.setAdapter(myAdapter);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

}
Run Code Online (Sandbox Code Playgroud)

我尝试更改 layoutmanager 但目前找不到解决方案...

如果您需要更多信息,请询问我,我会提供您需要的一切。

谢谢你的帮助 !

nsc*_*012 6

您得到的是 ,NPE因为RecyclerView位于 的布局中Fragment,但代码正在 . 的布局中寻找它Activity

这条线

RecyclerView myRv = (RecyclerView) findViewById(R.id.recyclerView_id);
Run Code Online (Sandbox Code Playgroud)

在此处设置的布局中寻找View具有 idrecyclerView_idactivity_main

setContentView(R.layout.activity_main);
Run Code Online (Sandbox Code Playgroud)

如果你想把RecyclerViewa放在里面Fragment,你需要把RecyclerView代码放在Fragment类里面。所以以下几行将进入Fragment课堂

RecyclerView myRv = (RecyclerView) findViewById(R.id.recyclerView_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,bo_movies);
myRv.setLayoutManager(new GridLayoutManager(this,3));
myRv.setAdapter(myAdapter);
Run Code Online (Sandbox Code Playgroud)

Fragment你将设置fragment_home为你的布局

public View onCreateView(...) {
    return inflater.inflate(R.layout.fragment_home, container, false);
}
Run Code Online (Sandbox Code Playgroud)