2014-12-01 64 views
0

我一直在試圖找出一段時間。我有一個使用操作欄的應用程序,但每次我想調用它時都會顯示null。給這個錯誤ActionBar給予nullPointerException?

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> 

,最後我的價值觀/樣式,如果您使用的是支持庫,那麼你的文件

<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> 

回答

1

必須使用getSupportActionBar()而不是getActionBar(),而不是擴展s Activity你必須延伸ActionBarActivityHere你可以找到文檔

0

你的getActionBar方法沒有返回任何東西我想,它是空的,所以它無法調用隱藏方法。

順便說一句,如果你甚至沒有從ActionBarActivity擴展你的Activity,你將如何直接使用getActionBar來獲得ActionBar引用?

1

你宣佈你AppTheme與隱藏的動作條: 使用這些標記,它永遠不會有

<!-- 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> 
-1

如果您正在使用支持庫將代碼放在這樣

public class Signup extends Activity 
{ 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.signup); 
     getSupportActionBar().hide(); 
     SetListeners(); 
    } 

.... 
+0

getSupportActionBar( )。隱藏();僅受AppCompact擴展支持 – Michael 2016-06-13 19:46:42