【发布时间】:2018-03-25 19:06:43
【问题描述】:
嗨,我正在尝试在单击 recyclerview 项目时调用 webview 片段,我用它的 webview 布局制作了一个片段,我有一个适配器来调用该片段,但它关闭了 APP:
public class WebViewFragment extends Fragment {
public WebView mWebView;
public WebViewFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mWebView = (WebView) mWebView.findViewById(R.id.webview);
mWebView.loadUrl("google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return inflater.inflate(R.layout.fragment_web_view, container, false);
}
}
还有适配器:
@Override
public void onClick(View view) {
int position = (int) view.getTag();
News news =newsItems.get(position);
Uri uri = Uri.parse(news.getLink());
Intent intent = new Intent(mACtivity, WebViewFragment.class);
mACtivity.startActivity(intent);
}
布局:
<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"
tools:context="tech.amro.amro.WebViewFragment">
<!-- TODO: Update blank fragment layout -->
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>
</FrameLayout>
我知道我还没有将 url 发送到 webview,但我只是在测试先加载视图然后发送 url。但是当单击该项目时,它会关闭 APP。并给出这个错误:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: tech.amro.amro, PID: 18958
android.content.ActivityNotFoundException: Unable to find explicit activity class {tech.amro.amro/tech.amro.amro.WebViewFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1932)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1615)
at android.app.Activity.startActivityForResult(Activity.java:4472)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4430)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4791)
at android.app.Activity.startActivity(Activity.java:4759)
at tech.amro.amro.adapters.NewsAdapter.onClick(NewsAdapter.java:73)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Application terminated.
【问题讨论】:
-
来自 logcat 的共享错误。 Intent 意图 = new Intent(mACtivity, WebViewFragment.class); WebViewFragment - 应该扩展活动,而不是片段
-
@user5599807 添加了错误。
-
您需要从活动扩展并在清单中声明活动:developer.android.com/training/basics/firstapp/…
-
@user5599807 但这不是活动,而是片段。
-
您不能使用 startActivity() 调用片段,因为这种方式仅适用于活动。所以你需要从活动扩展(检查我在之前评论中提供的链接)或以这种方式使用片段:developer.android.com/guide/components/fragments.html
标签: android android-fragments android-intent webview