android - “WebView 不能为空”的特殊情况
问题描述
我正在尝试在此代码中通过单击打开第二个活动和链接的按钮来打开网站。
我的第一个活动代码:
package com.medanis.fneclis
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button1.setOnClickListener{
val intent = Intent(this, License::class.java)
// To pass any data to another activity
intent.putExtra("targetURL", "www.firstlink.com")
// start your SecondActivity
startActivity(intent)
}
button2.setOnClickListener{
val intent = Intent(this, License::class.java)
// To pass any data to another activity
intent.putExtra("targetURL", "www.secondlink.com")
// start your SecondActivity
startActivity(intent)
}
button3.setOnClickListener{
val intent = Intent(this, License::class.java)
// To pass any data to another activity
intent.putExtra("targetURL", "www.thirdlink.com")
// start your SecondActivity
startActivity(intent)
}
}
}
我的第二个活动代码:
package com.medanis.fneclis
import android.content.Intent
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.webkit.ClientCertRequest
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_license.*
import kotlinx.android.synthetic.main.activity_license.view.*
import java.net.URL
class License : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_license) // replace with you xml file name webview
var url : String = intent.getStringExtra("value")?:""
webv.webViewClient = WebViewClient()
startActivity(intent)
}
}
许可证布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/bg2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fneclis_phone_bg_5"
android:orientation="vertical"
android:theme="@style/AppFullScreenTheme"
tools:context=".License">
<Button
android:id="@+id/wvbtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="316dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="316dp"
android:layout_marginStart="18dp"
app:layout_constraintBottom_toTopOf="@+id/webv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<WebView
android:id="@+id/webv"
android:layout_width="0dp"
android:layout_height="513dp"
android:layout_marginTop="36dp"
android:background="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/wvbtn">
</WebView>
</android.support.constraint.ConstraintLayout>
MAINACTIVITY 布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fneclis_phone_bg_3"
android:orientation="vertical"
android:theme="@style/AppFullScreenTheme"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="263dp"
android:layout_height="116dp"
android:layout_marginBottom="25dp"
android:layout_marginTop="13dp"
android:background="@drawable/fneclis_btn"
android:text="Next Page"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button3"
android:layout_width="263dp"
android:layout_height="116dp"
android:layout_marginBottom="76dp"
android:layout_marginTop="400dp"
android:background="@drawable/fneclis_btn"
android:text="Next Page"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="263dp"
android:layout_height="116dp"
android:layout_marginBottom="346dp"
android:layout_marginTop="130dp"
android:background="@drawable/fneclis_btn"
android:text="Next Page"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
错误信息:
java.lang.RuntimeException:无法启动活动 ComponentInfo{com.medanis.fneclis/com.medanis.fneclis.License}:java.lang.IllegalStateException:webv 不能为空
解决方案
实际上你的代码有很多问题,首先是背景,摆脱它。
这就是您的 activity_license.xml 的外观:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/bg2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/AppFullScreenTheme"
tools:context=".License">
<WebView
android:id="@+id/webv"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</WebView>
</android.support.constraint.ConstraintLayout>
其次,更改整个许可证活动中的代码,您的 onCreate 必须如下所示:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_license)
val url: String = intent.getStringExtra("targetURL") ?: ""
val webView = findViewById<WebView>(R.id.webv)
webView.loadUrl(url)
}
请在您的按钮点击逻辑上添加实际的网址,如下所示:
button1.setOnClickListener{
val intent = Intent(this, License::class.java)
intent.putExtra("targetURL", "https://www.google.com")
startActivity(intent)
}
推荐阅读
- jquery - 轮播上的页脚重叠
- java - Base 64 到 AES 128 中的现有 DB 列
- python - 熊猫:在数据框中重命名“未命名:*”或“NaN”
- python - 如何使用函数参数作为类型提示?
- java - 如何处理动态生成的 json 名称字段?
- pug - 在 pug 模板引擎中是否有类似 datalist(html 5) 或类似功能的等效功能?
- powershell - 在 powershell 的文件大小输出中包含 unc 路径
- c - 使用 SDL2、openGL ES 2.0 (GLSL 1.0) 和 Freetype 渲染字体
- angular - Angular 6: void 类型不存在订阅
- angular - Angular4无法读取未定义的属性“列表”