При переходе по кнопке из одного активити в другой, приложение рушится
Всем привет! Пишу приложение для нужд компании, пишу первый раз. В главном активити (AuthActivity) есть кнопка для перехода на вторую активити (MainActivity). После нажатия на эту кнопку приложение закрывается с ошибкой. В логе приложения вылетает много ошибок, с ссылкой на "setContentView(R.layout.activity_main)" в MainActivity. Также есть замечания к "bottomNavigationView.setOnNavigationItemSelectedListener(navListener)" и "BottomNavigationView.OnNavigationItemSelectedListener" эти методы почему-то перечеркнуты.
Файл Манифест:
<?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.istrcheese">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.IstrCheese"
tools:targetApi="31">
<activity android:name=".MainActivity" />
<activity
android:name=".AuthActivity"
android:exported="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
Логи приложения:
2022-08-20 21:36:46.987 24043-24048/com.example.istrcheese I/zygote: Do partial code cache collection, code=30KB, data=22KB
2022-08-20 21:36:46.988 24043-24048/com.example.istrcheese I/zygote: After code cache collection, code=30KB, data=22KB
2022-08-20 21:36:46.988 24043-24048/com.example.istrcheese I/zygote: Increasing code cache capacity to 128KB
2022-08-20 21:36:47.342 24043-24075/com.example.istrcheese D/OpenGLRenderer: HWUI GL Pipeline
2022-08-20 21:36:47.519 24043-24075/com.example.istrcheese D/: HostConnection::get() New Host Connection established 0xa3ca1740, tid 24075
2022-08-20 21:36:47.536 24043-24075/com.example.istrcheese I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2022-08-20 21:36:47.536 24043-24075/com.example.istrcheese I/OpenGLRenderer: Initialized EGL, version 1.4
2022-08-20 21:36:47.536 24043-24075/com.example.istrcheese D/OpenGLRenderer: Swap behavior 1
2022-08-20 21:36:47.537 24043-24075/com.example.istrcheese W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2022-08-20 21:36:47.537 24043-24075/com.example.istrcheese D/OpenGLRenderer: Swap behavior 0
2022-08-20 21:36:47.547 24043-24075/com.example.istrcheese D/EGL_emulation: eglCreateContext: 0xa6b050c0: maj 2 min 0 rcv 2
2022-08-20 21:36:47.592 24043-24075/com.example.istrcheese D/EGL_emulation: eglMakeCurrent: 0xa6b050c0: ver 2 0 (tinfo 0xa6b03300)
2022-08-20 21:36:47.635 24043-24048/com.example.istrcheese I/zygote: Do partial code cache collection, code=60KB, data=43KB
2022-08-20 21:36:47.637 24043-24048/com.example.istrcheese I/zygote: After code cache collection, code=60KB, data=43KB
2022-08-20 21:36:47.638 24043-24048/com.example.istrcheese I/zygote: Increasing code cache capacity to 256KB
2022-08-20 21:36:47.640 24043-24075/com.example.istrcheese D/EGL_emulation: eglMakeCurrent: 0xa6b050c0: ver 2 0 (tinfo 0xa6b03300)
2022-08-20 21:36:47.745 24043-24043/com.example.istrcheese V/View: dispatchProvideAutofillStructure(): not laid out, ignoring 0 children of 1073741833
2022-08-20 21:36:47.750 24043-24043/com.example.istrcheese I/AssistStructure: Flattened final assist data: 3040 bytes, containing 1 windows, 11 views
2022-08-20 21:37:10.144 24043-24043/com.example.istrcheese D/AndroidRuntime: Shutting down VM
2022-08-20 21:37:10.146 24043-24043/com.example.istrcheese E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.istrcheese, PID: 24043
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.istrcheese/com.example.istrcheese.MainActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class androidx.fragment.app.FragmentContainerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: java.lang.RuntimeException: Exception inflating com.example.istrcheese:navigation/nav_host_fragment_mainpage line 13
at androidx.navigation.NavInflater.inflate(NavInflater.kt:70)
at androidx.navigation.NavController.setGraph(NavController.kt:1039)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.kt:155)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:3090)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:257)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1890)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1814)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1720)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:323)
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.kt:158)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:53)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:136)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:248)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:227)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.istrcheese.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
2022-08-20 21:37:10.147 24043-24043/com.example.istrcheese E/AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.UnsupportedOperationException: Cannot add action 2131362285 to Destination(com.example.istrcheese:id/authActivity) label=activity_auth class=com.example.istrcheese.AuthActivity as it does not support actions, indicating that it is a terminal destination in your navigation graph and will never trigger actions.
at androidx.navigation.NavDestination.putAction(NavDestination.kt:445)
at androidx.navigation.NavInflater.inflateAction(NavInflater.kt:322)
at androidx.navigation.NavInflater.inflate(NavInflater.kt:107)
at androidx.navigation.NavInflater.inflate(NavInflater.kt:114)
at androidx.navigation.NavInflater.inflate(NavInflater.kt:64)
... 37 more
2022-08-20 21:37:10.156 24043-24048/com.example.istrcheese I/zygote: Do full code cache collection, code=116KB, data=106KB
2022-08-20 21:37:10.156 24043-24048/com.example.istrcheese I/zygote: After code cache collection, code=111KB, data=70KB
AuthActivity:
package com.example.istrcheese;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class AuthActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
Button button_login = findViewById(R.id.buttonLogin);
button_login.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
MainActivity:
package com.example.istrcheese;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav_menu);
bottomNavigationView.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, new HomeFragment()).commit();
}
@SuppressLint("NonConstantResourceId")
private final BottomNavigationView.OnNavigationItemSelectedListener navListener =
item -> {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.homeFragment:
selectedFragment = new HomeFragment();
break;
case R.id.reportingFragment:
selectedFragment = new ReportingFragment();
break;
case R.id.mapFragment:
selectedFragment = new MapFragment();
break;
case R.id.profileFragment:
selectedFragment = new ProfileFragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, selectedFragment).commit();
return true;
};
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
app:menu="@menu/menu_bottom_mainpage" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_nav_menu"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_host_fragment_mainpage" />
</RelativeLayout>
activity_auth:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AuthActivity">
<TextView
android:id="@+id/textAuth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="125dp"
android:text="@string/text_Auth"
android:textColor="@color/black"
android:textSize="23sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="35dp"
android:layout_marginTop="120dp"
android:autofillHints=""
android:ems="10"
android:hint="@string/text_Email"
android:inputType="textEmailAddress"
android:minHeight="48dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="35dp"
android:layout_marginTop="15dp"
android:autofillHints=""
android:ems="10"
android:hint="@string/text_Password"
android:inputType="textPassword"
android:minHeight="48dp" />
<TextView
android:id="@+id/textForgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:text="@string/text_ForgetPassword"
android:textColor="@color/black" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
android:text="@string/textButton_Login"
tools:ignore="TextContrastCheck" />
</LinearLayout>
Я нашёл решение на свой вопрос. После того как я поменял в файле навигации на переход на другой Активити происходит. Я не знаю верно ли так?)
Но теперь при переходе между фрагментами bottomnavigationview программа завершается с ошибкой с ссылкой на « bottomNavigationView.setOnNavigationItemSelectedListener(navListener);», она перечеркнута Как решить эту проблему?
Навигационный граф:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_host_fragment_mainpage"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/authActivity"
android:name="com.example.istrcheese.AuthActivity"
android:label="activity_auth"
tools:layout="@layout/activity_auth">
<action
android:id="@+id/action_authActivity_to_homeFragment"
app:destination="@id/homeFragment" />
<action
android:id="@+id/action_authActivity_to_forgetPassword"
app:destination="@id/forgetPasswordFragment" />
</fragment>
<fragment
android:id="@+id/homeFragment"
android:name="com.example.istrcheese.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/mapFragment"
android:name="com.example.istrcheese.MapFragment"
android:label="fragment_map"
tools:layout="@layout/fragment_map" />
<fragment
android:id="@+id/profileFragment"
android:name="com.example.istrcheese.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile">
<action
android:id="@+id/action_profileFragment_to_authActivity2"
app:destination="@id/authActivity" />
</fragment>
<fragment
android:id="@+id/reportingFragment"
android:name="com.example.istrcheese.ReportingFragment"
android:label="fragment_reporting"
tools:layout="@layout/fragment_reporting" />
<fragment
android:id="@+id/forgetPasswordFragment"
android:name="com.example.istrcheese.ForgetPasswordFragment"
android:label="fragment_forget_password"
tools:layout="@layout/fragment_forget_password" />
</navigation>