提问人:Dale Stark Summers 提问时间:6/7/2023 最后编辑:Jose Manuel de FrutosDale Stark Summers 更新时间:6/11/2023 访问量:412
无法从 Android Studio 中的主 Activity 启动底部导航视图 Activity
Unable to start a Bottom Navigation View Activity from Main Activity in Android Studio
问:
无法从 Android Studio 中的 Main Activity 启动底部导航视图 Activity。
在Android Studio(JAVA)上做一个项目时,我想在登录页面后创建一个提要。但是,当我尝试从其他一些活动启动包含底部导航栏的活动时,应用程序崩溃了。但是当我尝试以 BottomNavigationView 的形式创建 MainActivity 时,该应用程序运行良好。我已经附上了我按下按钮开始活动的logcat详细信息。
洛格猫
FATAL EXCEPTION: main Process: com.example.traceback, PID: 23707 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.traceback/com.example.traceback.homefeed}: java.lang.IllegalStateException: Activity com.example.traceback.homefeed@bc417d4 does not have an ActionBar set via setSupportActionBar() at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3645) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) 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:2307) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7872) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: java.lang.IllegalStateException: Activity com.example.traceback.homefeed@bc417d4 does not have an ActionBar set via setSupportActionBar() at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.kt:37) at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.kt:85) at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.kt:402) at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.kt:284) at com.example.traceback.homefeed.onCreate(homefeed.java:43) at android.app.Activity.performCreate(Activity.java:8305) at android.app.Activity.performCreate(Activity.java:8284) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3626) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) 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:2307) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7872) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
主页:.java
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.traceback.databinding.ActivityHomefeedBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class homefeed extends AppCompatActivity {
private ActivityHomefeedBinding binding;
private Toolbar tbar;
@Override
public void setSupportActionBar(@Nullable Toolbar toolbar) {
super.setSupportActionBar(toolbar);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tbar=(Toolbar)findViewById(R.id.toolbar);
super.setSupportActionBar(tbar);
binding = ActivityHomefeedBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_homefeed);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
}
}
登录页面.java
package com.example.traceback;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class loginPage extends AppCompatActivity implements View.OnClickListener {
TextView loginres;
Button logButton;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
loginres = (TextView) findViewById(R.id.loginRegis);
loginres.setPaintFlags(loginres.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
loginres.setOnClickListener(this);
logButton = (Button) findViewById(R.id.loginButton);
logButton.setOnClickListener(this);
}
@Override
public void onBackPressed(){
finish();
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.loginRegis){
finish();
startActivity(new Intent(this, registerPage.class));
}
else if(v.getId()==R.id.loginButton){
startActivity(new Intent(this, homefeed.class));
}
}
}
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">
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TraceBack"
tools:targetApi="31">
<activity
android:name=".register2"
android:exported="false" />
<activity
android:name=".registerPage"
android:exported="false"
android:windowSoftInputMode="adjustResize" />
<activity
android:name=".loginPage"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".homefeed" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
答:
1赞
Dale Stark Summers
6/9/2023
#1
我的主要主题在主题.xml中设置为 NoActionBar。在 AndroidManifest(phrasefix).xml 中将 homefeed 活动主题单独更改为 NoActionBar 后,问题得到了解决。
评论