我正在尝试用此代码通过单击打开第二个活动和链接的按钮来打开网站。
我的第一个活动代码:
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不能为空
答案 0 :(得分:0)
这段精确的代码可以正常工作,没有错误
Context contect=MainActivity.this;
TextView tview=new TextView(context);
tview.setPaddingRelative(10,0,0,0);
答案 1 :(得分:0)
实际上,您的代码有很多错误,首先要消除背景。
这是您的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)
}