这是我的工具栏xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbarxmlns:android="http://schemas.android.com /apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#affa0505"
android:elevation="4sp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light"
app:titleTextColor="#fff"
>
并且我已将此文件添加到mainActivity
<include layout="@layout/toolbar"
android:id="@+id/toolbar"
/>
这是我的菜单文件
<menu 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"
tools:context="com.example.shery.youtubevideodownloader.Main2Activity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:icon="@drawable/share"
app:showAsAction="ifRoom"
android:title="@string/action_settings" />
</menu>
这是我的java文件
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main2, menu);
return true;
}
,错误是
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shery.youtubevideodownloader, PID: 25483
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shery.youtubevideodownloader/com.example.shery.youtubevideodownloader.SplashScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.hide()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.hide()' on a null object reference
at com.example.shery.youtubevideodownloader.SplashScreen.onCreate(SplashScreen.java:17)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
I / Ads:应用测量正在启动,版本:13001
当我打开我的应用程序时我不知道我的项目有什么错误,我的应用程序崩溃了,我想添加工具栏
这是我的spalshScreen代码
package com.example.shery.youtubevideodownloader;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
public class SplashScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getSupportActionBar().hide();
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREENWindowManager.LayoutParams.FLAG_FULLSCREEN);,
setContentView(R.layout.activity_splash_screen);
Thread th = new Thread(){
@Override
public void run() {
super.run();
try {
//You Can Increase Or Decrease The Timing Of Splash Screen By Adding the number on sleep.
sleep(2000);
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
finish();
}catch (Exception e){
e.printStackTrace();
}
}
};
th.start();
}
}
答案 0 :(得分:0)
在onCreate()
中,您致电
getSupportActionBar().hide();
为什么?去掉它。
您不应该使用默认的ActionBar
,因为您需要工具栏。
如果您在styles.xml
中的主题是
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
将其更改为:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
答案 1 :(得分:0)
public class SplashScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREENWindowManager.LayoutParams.FLAG_FULLSCREEN);,
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
finish();
}catch (Exception e){
e.printStackTrace();
}
}
},2000);
}
}
答案 2 :(得分:0)
首先,如果要隐藏工具栏,那么为什么要将工具栏添加到xml中?
无论如何,请执行以下操作:
在您的toolbar.xml中,在工具栏和xmlns之间放置一个空格。
在工具栏中添加android.support.v7.widget.Toolbar
的ID
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com /apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="#affa0505" android:elevation="4sp" android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light" app:titleTextColor="#fff" >
android:id="@+id/toolbar"
。setSupportActionBar
方法:requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREENWindowManager.LayoutParams.FLAG_FULLSCREEN);, setContentView(R.layout.activity_splash_screen); setSupportActionBar(toolbar); getSupportActionBar().hide();