I have a recycler view and a XML file for the recycler view. I have used the recycler views inside the fragment layout. I want to access the textview id from the fragment activity for using fragment transactions.
var titleTextView = view.findViewById<TextView>(R.id.tvTitle)
val subjectFragment = SubjectFragment()
println(titleTextView.text)
the above code provides a null pointer exception
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.samachar, PID: 7038
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.example.samachar.NewsFragment.onViewCreated(NewsFragment.kt:86)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:248)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
at android.app.Activity.performStart(Activity.java:8024)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 7038 SIG: 9
I want to use the fragment manager to use the transactions and switch layouts
// var fragment = parentFragmentManager?.beginTransaction()
// fragment?.replace(R.id.newsFrameLayout, subjectFragment)
// fragment?.commit()
but I cannot seem to access the textview id from the fragment class.
<?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:id="@ id/newsItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="16dp"
android:paddingVertical="12dp">
<TextView
android:id="@ id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/title_eg"
android:textColor="@color/purple_200"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/tvDescription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/description_eg"
android:textSize="12sp"
android:textColor="@color/black"
android:maxLines="2"
android:justificationMode="inter_word"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/tvTitle" />
<TextView
android:id="@ id/tvCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/teal_700"
android:padding="2dp"
android:text="@string/category_eg"
android:textColor="@color/white"
android:textSize="10sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/tvDescription" />
<TextView
android:id="@ id/tvPublishedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pub_date_eg"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="@ id/tvCategory"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@ id/tvDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the xml layout used in the recycler view of the app
EDIT***
Solved it by using the onClickListener on textview in the adapter class onBindViewHolder function and used activity context for calling the supportFragmentManager.
CodePudding user response:
When you use a recyclerview there isn't a unique tvtitle id available. For each item in the recyclerview there is a tvtitle. Although you can aceess each of them from the recyclerview adapter.
CodePudding user response:
If you want to access any item of recyclerview then you need to create a event at onBindViewHolder . Use interface or lambda funtion for getting every view inside activity or fragment.
if you want views when click event happen then implement clickListener.