xinli/xinli-App/闪退问题排查.md
2025-11-23 10:19:32 +08:00

110 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 应用闪退问题排查指南
## 常见闪退原因
### 1. 布局文件问题
- 检查 `app/src/main/res/layout/activity_main.xml` 是否存在
- 检查所有 ID 是否匹配
### 2. WebView 初始化问题
- WebView 在某些设备上需要特殊处理
- 已添加异常处理和空值检查
### 3. 网络权限问题
- 确保 AndroidManifest.xml 中有网络权限
- 确保 `usesCleartextTraffic="true"` 已设置
### 4. 资源文件问题
- 检查图标文件是否存在
- 检查字符串资源是否存在
## 调试方法
### 方法一:查看 Logcat 日志
1. 在 Android Studio 中打开 **Logcat**
2. 过滤级别选择 **Error**
3. 运行应用,查看错误信息
4. 常见的错误信息:
- `NullPointerException` - 空指针异常
- `ClassNotFoundException` - 类找不到
- `Resources$NotFoundException` - 资源找不到
### 方法二:使用 adb logcat
```bash
adb logcat | findstr "AndroidRuntime"
```
### 方法三:检查崩溃报告
在 Android Studio 的 Logcat 中查找:
```
FATAL EXCEPTION: main
```
## 已修复的问题
1. ✅ 添加了异常处理try-catch
2. ✅ 添加了空值检查
3. ✅ 添加了视图初始化验证
4. ✅ 添加了 WebView 错误处理
5. ✅ 延迟了 URL 加载(避免模拟器网络问题)
## 可能的问题和解决方案
### 问题1模拟器无法访问局域网IP
**原因**模拟器运行在虚拟网络中无法直接访问主机的局域网IP
**解决方案**
- 使用 `10.0.2.2` 代替 `127.0.0.1``localhost` 来访问主机
- 或者使用真实设备测试
- 或者配置端口转发:
```bash
adb reverse tcp:8080 tcp:8080
```
### 问题2WebView 加载失败导致闪退
**已修复**:添加了错误处理,加载失败时显示错误信息而不是闪退
### 问题3资源文件缺失
**检查**
- `app/src/main/res/mipmap-*/ic_launcher.png` 是否存在
- `app/src/main/res/values/strings.xml` 是否存在
## 测试步骤
1. **清理并重新构建**
```bash
gradlew.bat clean
gradlew.bat assembleDebug
```
2. **卸载旧版本**
```bash
adb uninstall com.xinli.app
```
3. **安装新版本**
```bash
adb install app\build\outputs\apk\debug\app-debug.apk
```
4. **查看日志**
```bash
adb logcat -c # 清空日志
adb logcat | findstr "xinli"
```
## 如果仍然闪退
请提供以下信息:
1. Logcat 中的完整错误堆栈
2. Android 版本
3. 设备类型(模拟器/真机)
4. 闪退发生的时机(启动时/点击按钮时/加载网页时)