我正在尝试运行我的项目,但是在尝试运行仿真器时,我一直收到InflationException,并且我尝试在哪个设备上运行都没有关系。我已经发布了用于主屏幕的XML代码以及我遇到的错误以及试图运行的Java类。
主屏幕XML文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeScreenActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/BottomNavigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemIconTint="@drawable/bottom_navigation_colors"
app:itemTextColor="@drawable/bottom_navigation_colors"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="labeled">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<ScrollView
android:layout_width="409dp"
android:layout_height="543dp"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false">
<TextView
android:id="@+id/bishopBlogPage"
android:layout_width="194dp"
android:layout_height="126dp"
android:layout_marginStart="6dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:background="@drawable/bishops_blog"
android:gravity="center|bottom"
android:onClick="goToBishopBlog"
android:text="@string/title_blog"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/eventsPage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/calendarPage" />
<TextView
android:id="@+id/newsfeedPage"
android:layout_width="194dp"
android:layout_height="126dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="7dp"
android:layout_marginRight="7dp"
android:background="@drawable/newsfeed"
android:gravity="center|bottom"
android:onClick="goToNewsFeed"
android:text="@string/title_newsfeed"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/assebmlyPage"
app:layout_constraintTop_toBottomOf="@+id/eventsPage" />
<TextView
android:id="@+id/calendarPage"
android:layout_width="194dp"
android:layout_height="126dp"
android:layout_marginStart="6dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:background="@drawable/calendar"
android:gravity="center|bottom"
android:onClick="goToCalendar"
android:text="@string/title_calendar"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/videosPage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/welcomeView" />
<TextView
android:id="@+id/videosPage"
android:layout_width="194dp"
android:layout_height="126dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="7dp"
android:layout_marginRight="7dp"
android:background="@drawable/videos"
android:gravity="center|bottom"
android:onClick="goToVideos"
android:text="@string/title_videos"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/calendarPage"
app:layout_constraintTop_toBottomOf="@+id/welcomeView" />
<TextView
android:id="@+id/eventsPage"
android:layout_width="194dp"
android:layout_height="126dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="7dp"
android:layout_marginRight="7dp"
android:background="@drawable/events"
android:gravity="center|bottom"
android:onClick="goToEvents"
android:text="@string/title_events"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/bishopBlogPage"
app:layout_constraintTop_toBottomOf="@+id/videosPage" />
<TextView
android:id="@+id/welcomeView"
android:layout_width="412dp"
android:layout_height="240dp"
android:layout_marginStart="-1dp"
android:layout_marginLeft="-1dp"
android:layout_marginBottom="8dp"
android:background="@drawable/welcome"
android:gravity="center|bottom"
android:onClick="goToWelcome"
android:text="@string/title_Welcome"
android:textColor="#FFFFFF"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/calendarPage"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/assebmlyPage"
android:layout_width="194dp"
android:layout_height="126dp"
android:layout_marginStart="7dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:background="@drawable/assembly_square"
android:gravity="center|bottom"
android:onClick="goToAssembly"
android:text="@string/title_Assembly"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toStartOf="@+id/newsfeedPage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bishopBlogPage" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Bottom Navigation XMLFile:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home"
android:onClick="goToHomeScreen"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_give"
android:icon="@drawable/ic_heart"
android:onClick="goToGive"
android:title="@string/title_give" />
<item
android:id="@+id/navigation_calendar"
android:icon="@drawable/ic_calendar"
android:onClick="goToCalendar"
android:title="@string/title_calendar" />
<item
android:id="@+id/navigation_lectionary"
android:icon="@drawable/ic_lectionary"
android:onClick="goToLectionary"
android:title="@string/title_lectionary" />
</menu>
Java类:
package com.example.synodofficeapp;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.annotation.NonNull;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
public class HomeScreenActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener OnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_give:
mTextMessage.setText(R.string.title_give);
return true;
case R.id.navigation_calendar:
mTextMessage.setText(R.string.title_calendar);
return true;
case R.id.navigation_lectionary:
mTextMessage.setText(R.string.title_lectionary);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BottomNavigationView navView = findViewById(R.id.BottomNavigation);
mTextMessage = findViewById(R.id.message);
//navView.setOnNavigationItemSelectedListener(OnNavigationItemSelectedListener);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.homescreen);
} else {
setContentView(R.layout.homescreen);
}
//this leaves the keyboard hidden on load
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
getSupportActionBar().setTitle(R.string.title_home); // for set actionbar title
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.side_menu, menu);
return true;
}
public void goToWelcome(MenuItem view){
Intent intent = new Intent(this, WelcomeScreenActivity.class);
startActivity(intent);
finish();
}
public void goToAssembly(View view) {
Intent intent = new Intent(this, AssemblyScreen.class);
startActivity(intent);
finish();
}
public void goToBishopBlog(View view) {
Intent intent = new Intent(this, BishopBlogScreen.class);
startActivity(intent);
finish();
}
public void goToCalendar(View view) {
Intent intent = new Intent(this, CalendarScreen.class);
startActivity(intent);
finish();
}
public void goToContactUs(MenuItem view) {
Intent intent = new Intent(this, ContactUsScreen.class);
startActivity(intent);
finish();
}
public void goToCreateAccount(MenuItem view) {
Intent intent = new Intent(this, CreateAccountActivity.class);
startActivity(intent);
finish();
}
public boolean goToELCA(MenuItem view) {
Intent intent = new Intent(this, ELCAActivity.class);
startActivity(intent);
finish();
return true;
}
public void goToELCACandidacy(MenuItem view) {
Intent intent = new Intent(this, ELCACandidacyActivity.class);
startActivity(intent);
finish();
}
public void goToEvents(View view) {
Intent intent = new Intent(this, EventsActivity.class);
startActivity(intent);
finish();
}
public void goToGive(MenuItem view) {
Intent intent = new Intent(this, GiveActivity.class);
startActivity(intent);
finish();
}
public void goToLectionary(View view) {
Intent intent = new Intent(this, LectionaryActivity.class);
startActivity(intent);
finish();
}
public void goToNewsFeed(View view) {
Intent intent = new Intent(this, NewsFeedActivity.class);
startActivity(intent);
finish();
}
public void goToPulpetSupply(MenuItem view) {
Intent intent = new Intent(this, PulpetSupplyActivity.class);
startActivity(intent);
finish();
}
public void goToSettings(MenuItem view) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
finish();
}
public void goToVideos(View view) {
Intent intent = new Intent(this, VideoActivity.class);
startActivity(intent);
finish();
}
}
错误:
Process: com.example.app, PID: 7241
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.HomeScreenActivity}: android.view.InflateException: Binary XML file line #12 in com.example.app:layout/homescreen: Binary XML file line #12 in com.example.app:layout/homescreen: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.view.InflateException: Binary XML file line #12 in com.example.synodofficeapp:layout/homescreen: Binary XML file line #12 in com.example.synodofficeapp:layout/homescreen: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
Caused by: android.view.InflateException: Binary XML file line #12 in com.example.synodofficeapp:layout/homescreen: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:854)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.synodofficeapp.HomeScreenActivity.onCreate(HomeScreenActivity.java:53)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)