#追記 該当ソース
◆AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#e56700</color> <color name="colorPrimaryDark">#B35900</color> <color name="colorAccent">#D81B60</color> </resources> ◆DashboardFragment.kt package com.HOGE.HOGEapprication.ui.dashboard import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.webkit.WebResourceRequest import android.webkit.WebView import android.webkit.WebViewClient import android.widget.TextView import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import com.HOGE.HOGEapprication.R class DashboardFragment : Fragment() { private lateinit var dashboardViewModel: DashboardViewModel override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { dashboardViewModel = ViewModelProviders.of(this).get(DashboardViewModel::class.java) val root = inflater.inflate(R.layout.fragment_dashboard, container, false) val textView: TextView = root.findViewById(R.id.text_dashboard) dashboardViewModel.text.observe(this, Observer { textView.text = it }) /* val myWebView: WebView = root.findViewById(R.id.webview_dashboard) myWebView.webViewClient = object : WebViewClient() { /*WebViewで新しいurlに対してloadingが走った時 API Level 24(= Android 7)未満稼働しない可能性あり*/ override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { return false // アプリからブラウザに飛ばせない } } myWebView.loadUrl("https://www.google.com") */ return root } } ◆DashboardViewModel.kt package com.HOGE.HOGEapprication.ui.dashboard import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel class DashboardViewModel : ViewModel() { private val _text = MutableLiveData<String>().apply { value = "This is dashboard Fragment" } val text: LiveData<String> = _text } ◆ACTIVITY_MAIN.XML <?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" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?attr/actionBarSize"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/nav_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="?android:attr/windowBackground" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" /> <fragment android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:layout_constraintBottom_toTopOf="@id/nav_view" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/mobile_navigation" /> </androidx.constraintlayout.widget.ConstraintLayout> ◆FRAGMENT_DASHBOARD.XML <?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" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_dashboard" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:textAlignment="center" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <!-- webページ表示--> <WebView android:id="@+id/webview_dashboard" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
お世話になります。
アンドロイドスタジオでLOLIPOP API22最小要件対策をしています。WEBVIEWを実行できず質問させて頂きます。
他apiでは正常に稼働しているのですがwebviewを追加すると、以下エラーが出てしまいます。既にほかのコードはデバッグできており以下コードがエラー原因コードである事は特定できています。以下のコードはもちろん通常のwebviewですのでAPIをあげれば正常に動作しますがapi22ではエラーとなります。
<WebView android:id="@+id/webview_home" android:layout_width="match_parent" android:layout_height="match_parent" />
エラー内容は以下です。
11-22 00:02:22.383 5432-5432/com.py.pyapprication D/AndroidRuntime: Shutting down VM 11-22 00:02:22.384 5432-5432/com.py.pyapprication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.py.pyapprication, PID: 5432 ception: Unable to start activity ComponentInfo{com.py.pyapprication/com.py.pyapprication.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class android.webkit.WebView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
ためしたこと
例えばapi29では正常に上記コードが稼働しエラーは発生してませんでした。
よろしくお願いします。
エラー再現用の最小限のプロジェクトを作成してソース/レイアウトをご提示願えますでしょうか.
了解しました。すべてかきます。
jimbe様 質問欄の一番上にソースを提示しました。ご確認宜しくお願い致します。
自分の中で試して以下が怪しいと思っているのですが、api22と29にどのように分岐してコードをかくべきかわからず困っています。数日かなりはまっておりお手数かけますがご教示ください。
val myWebView: WebView = root.findViewById(R.id.webview_dashboard)
myWebView.webViewClient = object : WebViewClient() {
/*WebViewで新しいurlに対してloadingが走った時 API Level 24(= Android 7)未満稼働しない可能性あり*/
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
return false // アプリからブラウザに飛ばせない
}
}
myWebView.loadUrl("https://www.google.com")
状況や異常表示内容は違うのですが, 同じく Lollipop での WebView でご質問されている方がいらっしゃいます.
https://teratail.com/questions/224749
検索によると, API21~22[Lollipop,Android5.0~5.1]の(エミュレータの?) WebView には以前から問題があるようですが, その問題の詳細や今日現在の修正状況は分かりません.
> https://stackoverflow.com/questions/41025200/android-view-inflateexception-error-inflating-class-android-webkit-webview/49024931
まだ折角ご提示いただいたコードを試していないのですが, もしやするとエミュレータ上での問題で, 実機では発生しない類かもしれません.
この記事みました。初期ロリポップのバグですよね。ぜひコード試してください。返信待ってます
テキトウに設定した API22 の エミュレータで動かしてみましたが, google は表示されないものの, 落ちません.
build.gradle, menu/bottom_nav_menu と navigation/mobile_navigation はどのような内容でしょうか.
build.gradle, menu/bottom_nav_menu と navigation/mobile_navigation はどのような内容でしょうか.
すべてデフォルトです。22で表示されないとなるとロリポップのバグではないのか。っきしょー。stoppedというエラーも出ない感じでしょうか?
アプリからブラウザに飛ばしたくないのでshouldOverrideUrlLoadingを利用しています。lolipopまで下げると悪さをしている気がします。
参考
https://qiita.com/tarumzu/items/c991d66747fd092ff0d9
例えばAPIを判定しIF文で処理分ける事はできる場合コードをご教示いただけますと幸いです。
当初ご提示の画面のようにダイアログが出て止まっていましたが, それは MainActivity の onCreate の以下コメント部分の所為でした.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
/*if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, DashboardFragment.newInstance())
.commitNow()
}*/
}
こちらでは android studio の新プロジェクトの生成の際に Fragment+ViewModel から生成しましたので, navigation 関係が無く, build.gradle(app) の dependencies に
def nav_version = "2.1.0"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
を追加し, app:menu="@menu/bottom_nav_menu" と app:navGraph="@navigation/mobile_navigation" は消しています.
> 例えばAPIを判定しIF文で処理分ける
Build.VERSION.SDK_INT のことでしょうか.
android studio の new Project で Bottom Navigation Activity から作り直しましたが, Dashboard に WebView を追加しても落ちず, DashboardFragment のコメントされていた WebView の個所を有効にすると, 画面下部の Dashboad を押した際に(Homeなら”This is home Fragment”と表示される範囲に)
> Webpage not available
>
> The webpage at https://www.google.com/
> could not be loaded because:
>
> net::ERR_CACHE_MISS
と表示されています.
あなたの回答
tips
プレビュー