【问题标题】:app crash while andding intent & setting battery level in textview in Fragment在 Fragment 的 textview 中设置意图和设置电池电量时应用程序崩溃
【发布时间】:2020-07-08 06:09:53
【问题描述】:

在 textview 中设置电池电量时出错 我制作了一个选项卡式活动和一个登录屏幕(安装应用程序后只会打开一次)如果未添加意图,此片段 1 与主活动应用程序中的选项卡 1 连接不会崩溃。如果在 textview 中未设置电池电量,应用程序也不会崩溃。

片段1

package com.example.failedproject

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
     class fragnent1 : Fragment() {
         var textView:TextView?=null
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val v = inflater.inflate(R.layout.fragment_1, container, false)
            activity!!.registerReceiver(batteryInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
            textView = v.findViewById(R.id.textView)
            return v
        }
        var batteryInfoReceiver: BroadcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val label = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
                textView!!.text = label.toString()
            }
        }
    }

fragnent1_layout

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="38sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

出现错误

错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.failedproject, PID: 7613
    java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in com.example.failedproject.fragnent1$batteryInfoReceiver$1@5fb6952
        at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1560)
        at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: kotlin.KotlinNullPointerException
        at com.example.failedproject.fragnent1$batteryInfoReceiver$1.onReceive(fragnent1.kt:25)
        at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1550)
        at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

请帮帮我

【问题讨论】:

    标签: android android-studio android-layout kotlin android-fragments


    【解决方案1】:

    您需要先初始化 Textview,然后再为其设置文本或与 TextView 相关的任何其他此类操作。在您的代码中,文本视图在注册广播接收器后被初始化。因此,您可能会立即收到 BATTERY_CHANGED 触发器并且您的 Textview 不会被初始化。

    【讨论】:

    • 我犯了一个错误,我在 textView = v.findViewById(R.id.textView) 处写了错误的 id textView 但 id 是 textview 所以我收到了错误..非常感谢您的回复
    【解决方案2】:

    将这一行 textView = v.findViewById(R.id.textView) 移到活动上方!!.registerReceiver(batteryInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))

    因为您正在为尚未找到的 textview 设置值。 首先初始化你的视图,然后使用它们。

    【讨论】:

    • 我犯了一个错误,我在 textView = v.findViewById(R.id.textView) 处写了错误的 id textView 但 id 是 textview 所以我得到了错误....非常感谢回复
    【解决方案3】:

    注意你的 TextView id。 在布局中,您声明 android:id="+@id/textview"。 但是您使用 textView = v.findViewById(R.id.textView) 来查找它。 它们是不同的。 所以你的 textView 将为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-31
      • 2017-08-27
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      相关资源
      最近更新 更多