Android ActionBar提供nullPointerException?
我一直在想这个问题。我有一个使用动作栏的应用程序,但每次我想调用它时都会出现空值。给这个错误Android ActionBar提供nullPointerException?,android,Android,我一直在想这个问题。我有一个使用动作栏的应用程序,但每次我想调用它时都会出现空值。给这个错误 12-01 10:54:11.499: E/AndroidRuntime(2385): FATAL EXCEPTION: main 12-01 10:54:11.499: E/AndroidRuntime(2385): Process: com.example.jobpool, PID: 2385 12-01 10:54:11.499: E/AndroidRuntime(2385): java.
12-01 10:54:11.499: E/AndroidRuntime(2385): FATAL EXCEPTION: main
12-01 10:54:11.499: E/AndroidRuntime(2385): Process: com.example.jobpool, PID: 2385
12-01 10:54:11.499: E/AndroidRuntime(2385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobpool/com.example.jobpool.Signup}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.os.Looper.loop(Looper.java:135)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-01 10:54:11.499: E/AndroidRuntime(2385): at java.lang.reflect.Method.invoke(Native Method)
12-01 10:54:11.499: E/AndroidRuntime(2385): at java.lang.reflect.Method.invoke(Method.java:372)
12-01 10:54:11.499: E/AndroidRuntime(2385): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-01 10:54:11.499: E/AndroidRuntime(2385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-01 10:54:11.499: E/AndroidRuntime(2385): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
12-01 10:54:11.499: E/AndroidRuntime(2385): at com.example.jobpool.Signup.onCreate(Signup.java:16)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.Activity.performCreate(Activity.java:5933)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-01 10:54:11.499: E/AndroidRuntime(2385): ... 10 more
我知道这已经在其他线程中讨论过了,但我还没有找到适合我的解决方案。我添加了app.support.v7库
以下是onCreate方法:
public class Signup extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
getActionBar().hide();
SetListeners();
}
....
这是舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jobpool"
android:versionCode="1"
android:versionName="1.0" >
android:Debuggable = "True"
android:VMsafemode ="True"
android:AllowBackup = "True"
android:AllowClearuserdate= "True"
android:HardwareAccelerated = "True"
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/logo_icon_large"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Signup"
android:label="signup" >
</activity>
<activity
android:name=".Dashboard"
android:label="dashboard" >
</activity>
<activity
android:name=".UserRegistration"
android:label="@string/title_activity_user_registration" >
</activity>
</application>
</manifest>
android:Debuggable=“True”
android:VMsafemode=“True”
android:AllowBackup=“True”
android:AllowClearuserdate=“True”
android:HardwareAccelerated=“True”
最后是我的值/样式文件
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowNoTitle">true</item> <!-- Hides the Action Bar -->
<item name="android:windowFullscreen">true</item> <!-- Hides the status bar -->
</style>
</resources>
真的
真的
如果使用支持库,则必须使用getSupportActionBar()
而不是getActionBar()
,并且必须扩展ActionBar活动而不是扩展Activity
。您可以找到文档您的getActionBar方法没有返回任何东西,我认为它是空的,因此无法调用hide方法
顺便问一下,如果您还没有从ActionBarActivity扩展您的活动,那么如何使用getActionBar直接获取ActionBar引用?您在声明应用主题时隐藏了ActionBar:
有了这些旗帜,它永远不会在那里
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowNoTitle">true</item> <!-- Hides the Action Bar -->
<item name="android:windowFullscreen">true</item> <!-- Hides the status bar -->
</style>
真的
真的
如果您正在使用支持库,请以这种方式输入代码
public class Signup extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
getSupportActionBar().hide();
SetListeners();
}
....
getSupportActionBar().hide();仅AppCompact extends支持