2019309020132 2022-04-10 09:40 采纳率: 0%
浏览 25

想问一下这个为什么运行之后会出现App闪退呀?

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

```kotlin
package com.example.myapplication98

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.content.*
import android.widget.Button
import android.app.AlertDialog
import android.view.View

class MainActivity : AppCompatActivity() {
    private var intLevel = 0
    private var intScale = 0
    private var mButton1: Button? = null
    private var d: AlertDialog? = null
    private val mBatInfoReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context,intent: Intent) {
            val action = intent.action
            if (Intent.ACTION_BATTERY_CHANGED == action) {
                intLevel = intent.getIntExtra("level", 0)
                intScale = intent.getIntExtra("scale", 100)
                onBatteryInfoReceiver(intLevel, intScale)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        mButton1 = findViewById<View>(R.id.Button) as Button
        mButton1!!.setOnClickListener {
            registerReceiver(mBatInfoReceiver,
                IntentFilter(Intent.ACTION_BATTERY_CHANGED)
            )
        }
    }

    fun onBatteryInfoReceiver(intLevel:Int, intScale:Int) {
        val d = AlertDialog.Builder(this@MainActivity).create()
            d.setTitle(R.string.str_dialog_title)
            d.setMessage(resources.getString(R.string.str_dialog_body) + intLevel * 100 / intScale + "%")
            d.setButton(resources.getString(R.string.str_button2),
                DialogInterface.OnClickListener { dialog, which ->
                    unregisterReceiver(mBatInfoReceiver)
                    d.dismiss()
                })
            d.show()
        }
    }



<resources>
    <string name="app_name">My Application98</string>
    <string name="str_title">如何获取电池电量</string>
    <string name="str_button1">按我取得电池电量</string>
    <string name="str_button2">返回</string>
    <string name="str_dialog_title">系统信息</string>
    <string name="str_dialog_body">电池电量:</string>
</resources>


```kotlin
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/Button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_title"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.2" />

    <Button
        android:id="@+id/Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_button1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.387"
        tools:ignore="DuplicateIds" />

</androidx.constraintlayout.widget.ConstraintLayout>


  • 写回答

1条回答 默认 最新

  • 不会写代码的猴子 Android领域优质创作者 2022-04-10 14:33
    关注

    你在logcat中看一下有没有闪图日志吧

    评论

报告相同问题?

问题事件

  • 创建了问题 4月10日

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿