android - splashscreen android 应用程序崩溃,没有进入第二个活动
问题描述
我正在构建我的应用程序崩溃的小型简单闪屏。我用谷歌搜索了很多,但找不到答案。它首先显示启动画面,然后进入崩溃的第二个屏幕。我给第二个屏幕代码提供了 3 秒的时间延迟,如下所示。
MainActivity.java
package com.example.musically;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.musically">
<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">
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
闪屏.java
package com.example.musically;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(
new Runnable() {
@Override
public void run() {
Intent splashIntent=new Intent(getApplicationContext(),MainActivity.class);
startActivity(splashIntent);
finish();
}
}
,3000);
}
}
activity_splash_screen
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashScreen"
android:background="#fff">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logomusically"
android:layout_centerInParent="true"
/>
</RelativeLayout>
样式.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
截屏
错误
Process: com.example.musically, PID: 28801
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.musically/com.example.musically.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3170)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3307)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2036)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7081)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:928)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48)
at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:103)
at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:204)
at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:228)
at com.example.musically.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:7258)
at android.app.Activity.performCreate(Activity.java:7249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3150)
解决方案
你应该检查 MainActivity。错误日志说您正在尝试设置一个不存在的 ActionBar 的标题。
推荐阅读
- javascript - 尝试在 Alexa 中捕获贷款金额和利率并计算 EMI
- javascript - 将本地 CSV 文件映射到远程服务器并读取 CSV 中的最新数据以填充图表
- javascript - 如何告诉纯 JavaScript 不断检查变量?
- python - 错误:来自 csv 的 Python Whatsapp 消息。汽车
- python - turtle() 不旋转多个球加属性错误
- graphics - 金属底纹语言纹理不会清晰
- adsense-api - AdMob EARNINGS (USD) 价值不是真实价值
- javascript - 如何在中间而不是底部显示数据,而其中一个数据的值有很大差异。highcharts.js
- amazon-web-services - 如何将对现有 VPC 的引用传递给 cloudformation 模板?
- python - 安装 ipykernel 后 Jupyter Notebook 内核错误