所以我正在开发一个带有View分页器的项目,该项目具有2个带有2个不同片段的选项卡。这两个片段中都有一个卡片视图。当我单击卡片视图中的一个项目时,它应该转到另一个片段以使用从回收者视图适配器传递的URL加载Web视图。我试图将URL从适配器传递到片段以加载Web视图,但是应用程序在模拟器中不断崩溃。我可能为此使用了错误的方法,或者程序中未包含某些内容。我真的需要帮助!
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();
}
});
以上是我尝试将URL传递给FragmentWeb类的代码。我尝试使用log.d进行检查,并且URL变量已经包含所有URL。我也尝试过fragmentWeb.getChildFragmentManager甚至fragmentWeb.getFragmentManager,但它们都不起作用,并不断给我带来不同的错误。
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();
}
}
以上是使用从适配器传递的URL加载网页的代码。
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)
答案 0 :(得分:1)
这里:
FragmentWeb fragmentWeb = new FragmentWeb();
fragmentWeb.setArguments(bundle);
fragmentWeb.getActivity().getSupportFragmentManager()
您正在fragmentWeb上调用“ getActivity()”,但是您刚刚在上面创建了fragmentWeb。该片段尚未在任何活动中加载,因此getActivity()返回null。 从周围的上下文中获取当前活动,例如RecyclerView的上下文,或者,如果您将Adapter声明为Fragment的内部类,则使用Fragment的getActivity()。