【问题标题】:Kotlin Android every button click closes appKotlin Android 每次点击按钮都会关闭应用程序
【发布时间】:2020-02-24 12:43:19
【问题描述】:

每次单击按钮时,我的应用都会关闭。这只是在我将正在执行的代码放入 onclick 事件后才开始发生的。在此之前,这只是一个祝酒词,而且效果很好。

我无法从调试输出中判断正面或反面:

Connected to the target VM, address: 'localhost:8600', transport: 'socket'
W/Settings: Setting device_provisioned has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
V/HiTouch_HiTouchSensor: User setup is finished.
I/HwViewRootImpl: removeInvalidNode all the node in jank list is out of time
V/AudioManager: querySoundEffectsEnabled...
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/BlockMonitor: dispatchingThrewException In MainThread
D/AndroidRuntime: Shutting down VM
I/QarthLog: [PatchStore] createDisableExceptionQarthFile
    [PatchStore] create disable file for com.example.dsfrs_soup_news uid is 10507
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.dsfrs_soup_news, PID: 22361
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:523)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 
     Caused by: java.io.IOException: Cleartext HTTP traffic to www.dsfire.gov.uk not permitted
        at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:124)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:462)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
        at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:732)
        at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:707)
        at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:297)
        at org.jsoup.helper.HttpConnection.get(HttpConnection.java:286)
        at com.example.dsfrs_soup_news.MainActivity$onCreate$1.onClick(MainActivity.kt:21)
        at android.view.View.performClick(View.java:7189)
        at android.view.View.performClickInternal(View.java:7163)
        at android.view.View.access$3500(View.java:821)
        at android.view.View$PerformClick.run(View.java:27579)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8147)
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 
I/Process: Sending signal. PID: 22361 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

这是我的主要活动代码:

package com.example.dsfrs_soup_news

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import org.jsoup.Jsoup
import java.io.IOException

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        try{
        // get reference to button
        val BtnGo = findViewById(R.id.BtnGo) as Button
        val TxtResults: TextView = findViewById(R.id.TxtResults) as TextView
// set on-click listener
        BtnGo.setOnClickListener {
            Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
        val doc = Jsoup.connect("http//www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35").get()
        val links = doc.select("tr td:first-of-type a")
            .map { col -> col.attr("href") } //href gets each link
        //for every link display the gumf, ................consider just showing todays, exit loop once total number of incidents has been displayed
        links.forEach { links ->
            //println("http://www.dsfire.gov.uk/News/Newsdesk/$links \n")
            TxtResults.append("http://www.dsfire.gov.uk/News/Newsdesk/$links \n")
            // work with each link, get details from incident page
            val sIncident = Jsoup.connect("http://www.dsfire.gov.uk/News/Newsdesk/$links").get()
            var aLLDeetz = sIncident.select("td").toString() //convert to string to allow tidying
            //got all the details of the shout, now replace useless charecters
            aLLDeetz = aLLDeetz.replace("<td>", "")
            aLLDeetz = aLLDeetz.replace("</td>", "")
            aLLDeetz = aLLDeetz.replace("<td width=\"259\">", "")
            aLLDeetz = aLLDeetz.replace(":\\n", ": ")
            aLLDeetz = aLLDeetz.trim()
            //display results
            TxtResults.append("Icident Details are: \n  $aLLDeetz")
            //println(message = "Icident Details are: \n $aLLDeetz")
            //get all information in the incident summary and convert to string for cleaning
            var IncSumm = sIncident.getElementById("IncidentDetailContainer").toString()
            //clean useless chars out of string
            IncSumm = IncSumm.replace("<p>", "")
            IncSumm = IncSumm.replace("</div>", "")
            IncSumm = IncSumm.replace("<br></p>", "")
            IncSumm = IncSumm.replace("<br>", "")
            IncSumm = IncSumm.replace("<div id=\"IncidentDetailContainer\">", "")
            IncSumm = IncSumm.replace("&nbsp;</p>", "")
            IncSumm = IncSumm.replace("</p>", " ")
            IncSumm = IncSumm.replace("<p align=\"justify\">", "")
            IncSumm = IncSumm.replace("  ", "")
            //display results
            //println(IncSumm)
            TxtResults.append(IncSumm)
            finish() // other posts have put this in to stop forced close
        }
        }
    }  catch (e: IOException) {
            Toast.makeText(this@MainActivity, "Error is $e.", Toast.LENGTH_SHORT).show()
        }//try bracket
        
    }

}

我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/results"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:id="@+id/TxtResults" android:layout_marginBottom="20dp" android:clickable="true"
            android:enabled="true" android:focusable="true"/>
    <Button
            android:text="@string/go"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/BtnGo" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/TxtResults" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" android:enabled="true"/>

</androidx.constraintlayout.widget.ConstraintLayout>

还有我的毕业典礼

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/results"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:id="@+id/TxtResults" android:layout_marginBottom="20dp" android:clickable="true"
            android:enabled="true" android:focusable="true"/>
    <Button
            android:text="@string/go"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/BtnGo" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/TxtResults" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" android:enabled="true"/>

</androidx.constraintlayout.widget.ConstraintLayout>

当我仅使用按钮和 textview 启动应用程序时,它可以正常显示吐司。

只要我添加了 jsoup 代码来抓取网站,当我单击按钮时它就会强制关闭。我已经调试过,但什么也没得到,查找错误时,我唯一能看到的是一些被告知要添加 finish() 的地方。

它仍然会发生,所以我想我会问知道的人。

我在 Android Studio 中完成了这个项目,也遇到了这个问题,但是我使用 IntelliJ 完成的这段代码创建了一个新的空 android 项目。

我知道这可能是一个愚蠢的错误并且很容易修复,但这是我使用 jsoup 的第一个应用程序(我尝试了 skrape{it},但可用的文档和支持较少。

我已经编辑了代码以包含我捕获错误的尝试,并且我还按照说明创建了 xml 文件,当我单击按钮时应用程序仍然只是退出。

还有其他想法吗?

我刚刚注意到的另一件事是,无论我将调试点放在哪里,应用程序都会在大约 10 秒后崩溃,我几乎没有时间在它退出之前单击进入。

这是否有助于阐明我的问题?

【问题讨论】:

  • 问题就在堆栈跟踪中:“不允许到 www.dsfire.gov.uk 的明文 HTTP 流量”。 JSoup 有许多方法可以出于多种原因抛出 IOExceptions。您必须抓住它们,否则您的应用将在您无法控制的情况下崩溃,例如网站不可用、用户与 Internet 的连接不佳等。
  • 非常感谢,所以只需在我的代码中捕获错误并相应地处理它。只是一件事,代码在 kotlin 文件(普通的 kotlin 项目......不是 android)中完美运行,奇怪应该有区别吗?不是吗?
  • 似乎与 Android 安全设置有关。我不熟悉这个。所以有一些解决方法。但是您仍然需要捕获 IOExceptions,因为它们可能发生的所有原因不在您的控制范围内。 stackoverflow.com/questions/58022495/…
  • 这能回答你的问题吗? Android 8: Cleartext HTTP traffic not permitted
  • 在连接之前不要对 URL 进行编码java.net.MalformedURLException: no protocol: http%3A%2F%2Fwww.dsfire.gov.uk 对于你的后续问题,不要在主线程上进行网络请求。

标签: android kotlin jsoup forceclose


【解决方案1】:

@Tenfour04,已正确指出错误以解决此问题。 在“res”文件夹中创建“xml”文件夹并创建“network_security_config.xml”文件

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">dsfire.gov.uk</domain> // add the domain name
    </domain-config>
</network-security-config>

在应用标签下的清单中,添加

 android:networkSecurityConfig="@xml/network_security_config"

希望对您有所帮助!

【讨论】:

  • 我这样做了,还输入了 try and catch,查看我编辑的代码,但是当我单击按钮时它仍然只是关闭应用程序...我是否尝试正确捕获异常?跨度>
猜你喜欢
  • 1970-01-01
  • 2020-07-14
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多