gpt4 book ai didi

android - RecyclerView : Attempt to invoke virtual method on a null object reference

转载 作者:行者123 更新时间:2023-12-04 03:00:10 25 4
gpt4 key购买 nike

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

 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);
}

}

我尝试更改布局管理器,但目前找不到解决方案...

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

谢谢你的帮助 !

最佳答案

您将收到 NPE因为 RecyclerViewFragment 的布局中, 但代码在 Activity 的布局中寻找它.

这条线

RecyclerView myRv = (RecyclerView) findViewById(R.id.recyclerView_id);

正在寻找 View ID 为 recyclerView_id在布局 activity_main设置在这里
setContentView(R.layout.activity_main);

如果你想把 RecyclerViewFragment 内,你需要把 RecyclerView Fragment 中的代码类(class)。所以以下几行将进入 Fragment类(class)
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作为你的布局
public View onCreateView(...) {
return inflater.inflate(R.layout.fragment_home, container, false);
}

关于android - RecyclerView : Attempt to invoke virtual method on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49906748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com