You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无报错Android应用崩溃问题排查(添加IP获取代码后)

问题分析与解决方案

在MainActivity中添加获取IP的代码后应用崩溃且无错误提示,结合你的代码逻辑,问题主要集中在权限缺失、视图查找错误、空指针风险这几个核心点,以下是具体排查和修复步骤:

一、添加必要权限

获取Wifi状态必须声明对应权限,在AndroidManifest.xml中添加:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Android 6.0+ 需额外添加动态权限声明 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

如果应用目标SDK版本≥23,还需在代码中动态申请权限,否则会直接触发崩溃。

二、修复视图查找错误(核心问题)

你的MainActivity使用了ViewBinding(ActivityMainBinding),但添加的代码用了findViewById(R.id.ipadd)。结合Navigation组件的布局结构,nav_host_fragment_content_main是用来加载Fragment的容器,很大概率ipadd这个TextView属于某个Fragment的布局,而非MainActivity的布局。这种情况下findViewById会返回null,调用setText()就会触发空指针崩溃。

方案1:将IP获取逻辑移到对应Fragment中

假设ipadd在nav_home对应的Fragment布局里,在该Fragment的onViewCreated方法中实现逻辑:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val binding = HomeFragmentBinding.bind(view)
    
    // 先检查权限
    if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_WIFI_STATE)
        != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.ACCESS_WIFI_STATE), 1001)
        return
    }

    val wifiManager = requireContext().getSystemService(Context.WIFI_SERVICE) as WifiManager
    val connectionInfo = wifiManager.connectionInfo
    // 替代过时的Formatter.formatIpAddress方法
    val ipAddress = byteArrayOf(
        (connectionInfo.ipAddress shr 0).toByte(),
        (connectionInfo.ipAddress shr 8).toByte(),
        (connectionInfo.ipAddress shr 16).toByte(),
        (connectionInfo.ipAddress shr 24).toByte()
    ).joinToString(".") { it.toUByte().toString() }
    
    binding.ipadd.text = ipAddress
}

方案2:若必须在MainActivity处理,确保视图存在且用ViewBinding获取

如果ipadd确实在activity_main.xml布局中,直接用已初始化的binding对象获取视图:

// 在MainActivity的onCreate方法中,替换原findViewById逻辑
binding.ipadd.text = ipAddress

三、规避空指针风险

即使权限和视图问题解决,以下场景仍可能导致崩溃:

  • Wifi未开启时,wifiManager.getConnectionInfo()返回无效信息
  • 设备未连接Wifi时,getIpAddress()返回0

添加空判断逻辑:

val wifiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager
val connectionInfo = wifiManager.connectionInfo
if (connectionInfo.ipAddress != 0) {
    val ipAddress = byteArrayOf(
        (connectionInfo.ipAddress shr 0).toByte(),
        (connectionInfo.ipAddress shr 8).toByte(),
        (connectionInfo.ipAddress shr 16).toByte(),
        (connectionInfo.ipAddress shr 24).toByte()
    ).joinToString(".") { it.toUByte().toString() }
    binding.ipadd.text = ipAddress
} else {
    binding.ipadd.text = "未连接Wifi"
}

四、替换过时方法

Formatter.formatIpAddress()已被标记为过时,建议使用上述字节数组转换的方式生成IP字符串,适配更高版本系统。

内容的提问来源于stack exchange,提问作者GLobeasd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 06:05:21