我正在尝试使用Android Studio 3.4.1运行应用程序,但是当我尝试调试或运行它时,该应用程序崩溃。
我已经遇到Unable to start activity ComponentInfo
错误,但是在使用Kotlin的项目(该项目使用Java)上,但有一个例外。我尝试过编辑清单,修改活动并将mipmap
文件夹中的所有图像都更改为drawable
文件夹,但是这些都不起作用。
这是我的AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.testapp">
<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"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2" />
<activity
android:name=".NotificationsActivity"
android:label="@string/title_notifications" />
<activity
android:name=".DashboardActivity"
android:label="@string/title_dashboard" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.testapp.DashboardActivity" />
<activity
android:name=".BaseActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我的BaseActivity.java
:
package com.example.testapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class BaseActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
protected BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId()); //References this line in the logs
navigationView = findViewById(R.id.nav_view);
navigationView.setOnNavigationItemSelectedListener(this);
}
@Override
protected void onStart() {
super.onStart();
updateNavigationBarState();
}
// Remove inter-activity transition to avoid screen tossing on tapping bottom navigation items
@Override
public void onPause() {
super.onPause();
overridePendingTransition(0, 0);
}
@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
navigationView.postDelayed(new Runnable() {
@Override
public void run() {
int itemId = item.getItemId();
if (itemId == R.id.navigation_home) {
BaseActivity.this.startActivity(new Intent(BaseActivity.this, BaseActivity.class));
} else if (itemId == R.id.navigation_dashboard) {
BaseActivity.this.startActivity(new Intent(BaseActivity.this, DashboardActivity.class));
} else if (itemId == R.id.navigation_notifications) {
BaseActivity.this.startActivity(new Intent(BaseActivity.this, NotificationsActivity.class));
}
BaseActivity.this.finish();
}
}, 300);
return true;
}
private void updateNavigationBarState(){
int actionId = getNavigationMenuItemId();
selectBottomNavigationBarItem(actionId);
}
void selectBottomNavigationBarItem(int itemId) {
Menu menu = navigationView.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem item = menu.getItem(i);
boolean shouldBeChecked = item.getItemId() == itemId;
if (shouldBeChecked) {
item.setChecked(true);
break;
}
}
}
int getContentViewId() {
return 0;
}
int getNavigationMenuItemId() {
return 0;
}
}
还有我的调试日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testapp, PID: 10261
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp/com.example.testapp.BaseActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2827)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2902)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1603)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6578)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:204)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2133)
at android.content.res.Resources.getLayout(Resources.java:1142)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.testapp.BaseActivity.onCreate(BaseActivity.java:18)
at android.app.Activity.performCreate(Activity.java:7016)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2902)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1603)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6578)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
该应用程序应该可以运行,但是在修改.javas之后,无论我做什么,它都开始出现此错误。我见过一些结果相似的问题,但没有一个问题对我有帮助。
答案 0 :(得分:0)
尝试更改
setContentView(getContentViewId());
到
setContentView(R.layout.activity_base);
错误:
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
表示找不到资源,因为它指向setContentView
,这意味着找不到getContentViewId()
。