我正试图使用Android Studio 3.4.1运行一个应用程序,但当我试图调试或运行它时,应用程序崩溃了。
我已经遇到过一次无法启动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>
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)
尝试改变
setContentView(getContentViewId());
到
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
任何帮助都是非常感谢的。提前谢了。
我是Android的初学者。我正在开发一个应用程序。我收到这个问题,我不知道怎么做。 这是我在logcat中收到的信息:
错误为: 11-17 06:23:34.603 354 0-3540/com.example.ruiru.salestracker E/AndroidRuntime:致命异常:主进程:com.example.ruiru.salestracker,pid:3540 java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.ru
请在这方面需要帮助。当我试图在模拟器上运行我的应用程序时,我的Logcat中出现了这些错误,我的UI崩溃了 logcat错误