Olá pessoal, estou desenvolvendo um app para fornercer informações sobre o covid.
Ele vai apresentar algumas páginas sobre virologia básica, cuidados e proteção etc.
Uma dessas páginas é o “Sobre o App” na qual eu coloco um button para direcionar
para um WebView que vai mostrar um questionário.
Tudo funciona bem, mas quando clico no button para o questionário o app fecha.
Guerreiros, vocês podem me ajudar? Sou iniciante e estou tentando não deixar o
código uma bagunça.
AndroidManifest
<?xml version="1.0" encoding="utf-8"?><uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/icon_covid" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".activity_about" /> <activity android:name=".activity_cure" /> <activity android:name=".activity_doubt" /> <activity android:name=".activity_covid" /> <activity android:name=".activity_cuidados" /> <activity android:name=".intro_virus" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
Layout .xml que traz o button para o questionário:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/textView41" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="7dp" android:gravity="center" android:text="Informações sobre o Aplicativo" android:textSize="30sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView42" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="30dp" android:text="@string/sobre_o_app" android:textColor="@android:color/black" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/buttonq" /> <TextView android:id="@+id/textView48" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20sp" android:text="@string/questionario" android:textColor="@android:color/black" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView41" /> <Button android:id="@+id/buttonq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Questionário" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView48" /> </androidx.constraintlayout.widget.ConstraintLayout>
Activity do layout:
package com.example.app_covid
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Buttonclass activity_about : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_about)val buttonq = findViewById<Button>(R.id.buttonq) buttonq.setOnClickListener { val intent = Intent(this, activity_questionario::class.java) startActivity(intent) } } }
Layout .xml do questionário:
<?xml version="1.0" encoding="utf-8"?><WebView android:id="@+id/WebView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" />
Activity do questionário:
class activity_questionario : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_questionario)val mWebView = findViewById<View>(R.id.WebView) as WebView mWebView.loadUrl("https://docs.google.com/forms/d/e/1FAIpQLScQM1owMbHTDP6NE28Gb1pSvPA0Qq7Rih-QUC5zrndNN_tC_Q/viewform") val webSettings = mWebView.settings webSettings.javaScriptEnabled = true mWebView.webViewClient = WebViewClient() mWebView.canGoBack() mWebView.setOnKeyListener(View.OnKeyListener{ v, keyCode, event -> if (keyCode == KeyEvent.KEYCODE_BACK && event.action == MotionEvent.ACTION_UP && mWebView.canGoBack()){ mWebView.goBack() return@OnKeyListener true } false }) }
}