Reputation: 31
i'm trying in this code to open a website by clicking in a button which open the second activity and a link.
my first activity Code :
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)
}
}
}
my Second activity Code :
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)
}
}
LIcense LAYout :
<?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 layout :
<?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>
error message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.medanis.fneclis/com.medanis.fneclis.License}: java.lang.IllegalStateException: webv must not be null
Upvotes: 2
Views: 2182
Reputation: 714
Actually there's a lot wrong with your code, firstly the background, get rid of that.
This is how the your activity_license.xml must look:
<?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>
Secondly, change the code in your entire License Activity, your onCreate must look like so:
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)
}
And please put actual urls on your button click logic, like so:
button1.setOnClickListener{
val intent = Intent(this, License::class.java)
intent.putExtra("targetURL", "https://www.google.com")
startActivity(intent)
}
Upvotes: 1
Reputation: 714
This exact piece of code works finr with no errors
val webview: WebView? = findViewById<WebView>(R.id.webv)
webview?.webViewClient = WebViewClient()
Upvotes: 0