当我尝试测试我的应用时遇到问题,我是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
以下是导致原因的文件:
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();
}
}
这是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>
这是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);
}
}
我尝试更改布局管理器,但目前无法找到解决方案......
如果您需要更多信息,请问我,我会提供您所需要的一切。
感谢您的帮助!
答案 0 :(得分:1)
您收到NPE
,因为RecyclerView
位于Fragment
的布局中,但代码正在Activity
的布局中查找。< / p>
这一行
RecyclerView myRv = (RecyclerView) findViewById(R.id.recyclerView_id);
正在寻找在此处设置的布局View
中标识为recyclerView_id
的{{1}}
activity_main
如果您想将setContentView(R.layout.activity_main);
放在RecyclerView
内,则需要将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);
中,您需要将Fragment
设置为布局
fragment_home