My App is crashing whenever I try to put a recyclerView inside a fragment.
I can't figure out what is going on. I tried using a button to replace the layout in Fragment. App was starting fine but whenever I click the button the App Crashes
MainActivity:
public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView=findViewById(R.id.recyclerView); FrameLayout frameLayout = new FrameLayout(this); frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)); frameLayout.setId(R.id.layout); setContentView(frameLayout); getSupportFragmentManager().beginTransaction().add(R.id.layout,new Fragment1()).commit(); }}
fragment class:
public class Fragment1 extends Fragment { String[] Text={"he","hdhd","hdhdhdh","dhlsd","hshdsss","he","hdhd"}; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = LayoutInflater.from(getContext()).inflate(R.layout.rows,container,false); RecyclerView recyclerView = view.findViewById(R.id.RecyclerView2); ImrulsAdapter Adapter = new ImrulsAdapter(getContext(),Text); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setAdapter(Adapter); return view; }}
Custom Adapter Class:
public class ImrulsAdapter extends RecyclerView.Adapter<ImrulsAdapter.ImrulsViewHolder> { String[]Text; Context context; public ImrulsAdapter(Context context, String[] text ) { Text = text; this.context = context; } @NonNull @Override public ImrulsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.rows,parent,false); return new ImrulsViewHolder(view); } @Override public void onBindViewHolder(@NonNull ImrulsViewHolder holder, int position) { holder.textView.setText(Text[position]); } @Override public int getItemCount() { return Text.length; } public class ImrulsViewHolder extends RecyclerView.ViewHolder{ TextView textView; public ImrulsViewHolder(@NonNull View itemView) { super(itemView); textView= itemView.findViewById(R.id.textView); } }}
activity_main 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" tools:context=".MainActivity" android:id="@+id/Constraint_Layout"><androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:id="@+id/recyclerView"/></androidx.constraintlayout.widget.ConstraintLayout>
Fragment xml:
<?xml version="1.0" encoding="utf-8"?><FrameLayout 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"><androidx.recyclerview.widget.RecyclerView android:layout_width = "match_parent" android:layout_height="wrap_content" android:id="@+id/RecyclerView2"></androidx.recyclerview.widget.RecyclerView></FrameLayout>
Adapter Rows 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="wrap_content"><androidx.cardview.widget.CardView android:id="@+id/cardView" android:layout_width="409dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"><androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"><TextView android:id="@+id/textView" style="@style/Widget.AppCompat.TextView.SpinnerItem" android:layout_width="407dp" android:layout_height="100dp" android:text="TextView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout></androidx.cardview.widget.CardView></androidx.constraintlayout.widget.ConstraintLayout>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.recyclerviewinsideafragment"><application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:largeHeap="true" android:hardwareAccelerated="false" android:theme="@style/Theme.RecyclerViewInsideAFragment"><activity android:name=".MainActivity" android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>Dependencies:plugins { id 'com.android.application'}android { compileSdk 31 defaultConfig { applicationId "com.example.recyclerviewinsideafragment" minSdk 21 targetSdk 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }}dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.recyclerview:recyclerview:1.3.0-alpha01' implementation 'androidx.constraintlayout:constraintlayout:2.1.1' implementation 'androidx.fragment:fragment:1.3.6' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'}