Android Studio修改启动主页面失败,启动BatteryStatusActivity报错
启动页切换为BatteryStatusActivity后报Resources$NotFoundException错误
将应用启动页从MainActivity改为BatteryStatusActivity,修改AndroidManifest.xml后启动应用时出现致命错误,错误日志如下:
E FATAL EXCEPTION: main Process: com.parrot.camera, PID: 28232 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parrot.camera/com.parrot.camera.BatteryStatusActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f0c001c
相关代码片段
AndroidManifest.xml
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="com.parrot.camera.MainActivity" android:configChanges="orientation" android:screenOrientation="landscape" android:exported="true"> </activity> <activity android:name="com.parrot.camera.BatteryStatusActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
BatteryStatusActivity核心代码
droneBatteryInfo = findViewById(R.id.droneBatteryTxt) remoteBatteryInfo = findViewById(R.id.remoteBatteryTxt) val flyViewBtn = findViewById<Button>(R.id.connectDroneBtn) flyViewBtn.setOnClickListener { val intent = Intent (this, MainActivity::class.java) startActivity(intent) } updateBatteryStatus()
解决方案
- 定位缺失资源:错误中的
Resource ID #0x7f0c001c是关键,打开app/build/intermediates/runtime_symbol_list/debug/R.txt文件,搜索该ID找到对应的资源名称,检查该资源是否存在于res目录下(比如布局、字符串、Drawable等)。 - 检查布局文件匹配:确认BatteryStatusActivity的
onCreate方法中调用了setContentView(R.layout.xxx),且对应的布局文件中包含droneBatteryTxt、remoteBatteryTxt、connectDroneBtn这三个ID,没有拼写错误或被误删。 - 重新编译项目:点击Android Studio顶部菜单
Build->Rebuild Project,重新编译资源,修复可能的资源ID不匹配问题。 - 清理缓存:点击
File->Invalidate Caches...-> 勾选Invalidate and Restart,清理缓存后重启项目,解决资源缓存导致的异常。
内容的提问来源于stack exchange,提问作者Ophelia
相关产品推荐
相关产品推荐

