So I am developing a project with view pager that has 2 tabs with 2 different fragments. There's a card view in both of the fragments. When I click on one of the item in the card view, it should go to another fragment to load the web view with URLs that are passed from the recycler view adapter. I tried to pass the URLs from the adapter to the fragment to load the web view but the app keep on crashing in the emulator. I might be using the wrong method for this or there's something I didn't include in the program. I really need help!
main_activty.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CYBERSECURITY" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AI" />
</com.google.android.material.tabs.TabLayout>
</androidx.viewpager.widget.ViewPager>
</LinearLayout>
fragment_web.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/webView_Frame"
tools:context=".FragmentWeb">
<!-- TODO: Update blank fragment layout -->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</WebView>
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
}
MyRecyclerViewAdapter.java
final String URLs = news.getURLs();
Log.d("here",URLs);
myViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString(KEY, URLs);
FragmentWeb fragmentWeb = new FragmentWeb();
fragmentWeb.setArguments(bundle);
fragmentWeb.getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.webView_Frame, fragmentWeb)
.commit();
}
});
Above are the codes I tried to pass the URLs to the FragmentWeb class. I tried with log.d to check and the URLs variable already contain all the URLs. I've also tried with fragmentWeb.getChildFragmentManager or even fragmentWeb.getFragmentManager but neither of them work and keep giving me different errors.
FragmentWeb.java
public class FragmentWeb extends Fragment{
private static final String KEY = "URLS";
private String URLs;
private ProgressBar progressBar;
private WebView webView;
public FragmentWeb() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
URLs = getArguments().getString(KEY);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_web, container, false);
progressBar = view.findViewById(R.id.progress_bar);
webView = view.findViewById(R.id.web_view);
return view;
}
public void loadWebPage()
{
webView.loadUrl(URLs);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String URLs, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
webView.setVisibility(View.GONE);
}
@Override
public void onPageFinished(WebView view, String URLs) {
progressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
});
}
@Override
public void onStart() {
super.onStart();
loadWebPage();
}
}
Above are the codes to load the web page with the URLs passed from the adapter.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.assignment4_task1, PID: 6803
java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.fragment.app.FragmentManager androidx.fragment.app.FragmentActivity.getSupportFragmentManager()' on a null object reference at com.example.assignment4_task1.MyRecyclerViewAdapter$1.onClick(MyRecyclerViewAdapter.java:66)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)