#java #android #xml #webview
Вопрос:
Когда я добавляю a WebView
, приложение принудительно останавливается. На самом деле, я не знаю, что в этом плохого. Сборка успешно завершена. Однако, когда я запускаю встроенное приложение, оно принудительно закрывается. Как я могу решить эту проблему? Я добавлю свой код.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intro">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Intro"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Intro.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Основная деятельность
private AppBarConfiguration mAppBarConfiguration;
private WebView webView;
private Intent web = new Intent(Intent.ACTION_VIEW);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://google.com");
}
fragment_intro.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"
tools:context=".ui.intro.IntroductionFragment">
<WebView
android:id="@ id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Комментарии:
1. Можете ли вы опубликовать ошибку logcat
2. ‘Webview’ присутствует в ‘fragment_intro.xml», в то время как в «основной активности» вы получаете его от «activity_main.xml’
3. Вы должны вставить свое веб-представление в свой activity_main.xml как уже упоминалось @Nitish
Ответ №1:
в MainActivity.java
private AppBarConfiguration mAppBarConfiguration;
private WebView webView;
private Intent web = new Intent(Intent.ACTION_VIEW);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_intro);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://google.com");
}
Если это не работает, пожалуйста, добавьте IntroductionFragment.java
свой код, так как он используется в вашем макете:
как tools:context=".ui.intro.IntroductionFragment