اذهب إلى المحتوى

السؤال

نشر

يحدث لدي الخطأ التالي:

ERROR Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager) ' on a null object reference at com.example.application.Fragments.Home.HomeFragment.onCreate(HomeFragment.java:32)

الشاشة الرئيسية:

class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigationView = findViewById(R.id.bottonNavigationView);
        loadFragment(new HomeFragment());
        setBottomNavigationView();
    }
    private void loadFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.idMainFrameLayout, fragment);
        fragmentTransaction.commit();
    }
    @SuppressLint("NonConstantResourceId")
    public void setBottomNavigationView() {
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.btnSearch:
                    loadFragment(new SearchFragment());
                    break;
                case R.id.btnFeed:
                    loadFragment(new FeedFragment());
                    break;
                case R.id.btnShedule:
                    loadFragment(new SheduleFragment());
                    break;
                case R.id.btnProfile:
                    loadFragment(new ProfileFragment());
                    break;
                default:
                    loadFragment(new HomeFragment());
            }
            return true;
        });
    }
}

كود الfragment:

class HomeFragment extends Fragment {
    private RecyclerView recyclerView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        recyclerView = container.findViewById(R.id.recycleViewHome);
        recyclerView.setAdapter(new MyAdapter(getMyList()));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        return view;
    }
    private ArrayList<Store> getMyList() {
        String mTitle = "Google", mSubTitle = "Description";
        int imageView = R.drawable.ic_google_svg;
        ArrayList<Store> storeArrayList = new ArrayList<>();
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        storeArrayList.add(new Store(imageView, mTitle, mSubTitle));
        return storeArrayList;
    }
}

واجهة الfragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".Fragments.Home.HomeFragment">
    <androidx.recyclerview.widget.RecyclerView
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:id="@+id/recycleViewHome"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="20dp"
        android:orientation="vertical"
        tools:listitem="@layout/card_layout_stores" />
</LinearLayout>

 

Recommended Posts

  • 0
نشر

سبب المشكلة هو السطر التالي في كود الـfragment:

recyclerView = container.findViewById(R.id.recycleViewHome);

لحل المشكلة يجب تعديل السطر ليصبح هكذا:

recyclerView = view.findViewById(R.id.recycleViewHome);

أي أننا إستخدما view بدلاً من container لجلب الـrecyclerView الخاص بنا.

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...