Android Fragments 使用导航活动在Recyclerview中膨胀类片段时出错

hmtdttj4  于 2022-11-24  发布在  Android
关注(0)|答案(1)|浏览(173)

我正在制作一个使用Android Studio来编写和调用消息的应用程序。在将回收器视图膨胀到片段中的过程中出现了此错误消息。
我怎么能修好它?为什么这个不能充气?
以下是错误消息...

java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.hnu.project/kr.hnu.project.NavigationActivity}: android.view.InflateException: Binary XML file line #11 in kr.hnu.project:layout/activity_navigation: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.view.InflateException: Binary XML file line #11 in kr.hnu.project:layout/activity_navigation: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void kr.hnu.project.MyRecyclerAdapter.notifyDataSetChanged()' on a null object reference
        at kr.hnu.project.ui.home.HomeFragment.setArray(HomeFragment.java:83)
        at kr.hnu.project.ui.home.HomeFragment.onCreateView(HomeFragment.java:52)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)

以及回收器视图应该看到的HomeFragment。

public class HomeFragment extends Fragment {
private final static String selectMsg = "SELECT sender, receiver, title, date, content FROM MessageDB";
RelativeLayout rel;
LinearLayout lin;
DBHelper dbHelper;
SQLiteDatabase readDB;
Cursor cursor;
ArrayList<MyItem> mailItem;
MyRecyclerAdapter myAdapter;

private FragmentHomeBinding binding;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    HomeViewModel homeViewModel =
            new ViewModelProvider(this).get(HomeViewModel.class);

    binding = FragmentHomeBinding.inflate(inflater, container, false);
    View root = binding.getRoot();
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_home, container, false);

    dbHelper = new DBHelper(inflater.getContext());
    readDB = dbHelper.getReadableDatabase();
    mailItem = new ArrayList<MyItem>();

    setArray();

    myAdapter = new MyRecyclerAdapter(getActivity(), mailItem);
    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(myAdapter);



    return root;
}

public void setArray() {
    cursor = readDB.rawQuery(selectMsg, null);
    mailItem.clear();

    while (cursor.moveToNext()) {

        mailItem.add(new MyItem(cursor.getString(cursor.getColumnIndexOrThrow("sender")), cursor.getString(cursor.getColumnIndexOrThrow("receiver")),
                cursor.getString(cursor.getColumnIndexOrThrow("title")), cursor.getString(cursor.getColumnIndexOrThrow("date")),
                cursor.getString(cursor.getColumnIndexOrThrow("content"))));

    }

    cursor.close();
   //myAdapter.notifyDataSetChanged();

}

这是具有home片段导航活动

public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private AppBarConfiguration mAppBarConfiguration;
private ActivityNavigationBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    dataToFragment(); 

    binding = ActivityNavigationBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    setSupportActionBar(binding.appBarNavigation.toolbar); 

    DrawerLayout drawer = binding.drawerLayout;
    NavigationView navigationView = binding.navView;
    navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this); 

    //
    mAppBarConfiguration = new AppBarConfiguration.Builder( 
            R.id.nav_send, R.id.nav_set, R.id.nav_home)
            .setOpenableLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_navigation);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

这是错误消息中的activity_navigation. xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    android:id="@+id/app_bar_navigation"
    layout="@layout/app_bar_navigation"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_navigation"
    app:menu="@menu/activity_navigation_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

这是错误消息中的内容_导航. xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_navigation">

<fragment
    android:id="@+id/nav_host_fragment_content_navigation"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" /

</androidx.constraintlayout.widget.ConstraintLayout>
这是homeFragment的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.home.HomeFragment">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>
bvjxkvbb

bvjxkvbb1#

因为我的声望低于50,我不能评论你的帖子,所以我会在这里评论。
这个方法有什么作用?dataToFragment();在扩大布局之前调用它

相关问题