android - ActionBar 给出 nullPointerException?

标签 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.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();
        }

....

这是 list :

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

最佳答案

如果您正在使用支持库,那么您必须使用 getSupportActionBar() 而不是 getActionBar(),而不是扩展 Activity你必须扩展 ActionBarActivityHere你可以找到文档

关于android - ActionBar 给出 nullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27235501/

相关文章:

java - 在 Activity A中连接到websocket并在 Activity B中接收数据

java - 房间数据库观察者

android - 如何从我的应用程序打开谷歌日历应用程序?

Android卸载 ListView 图像

android - 在库模块中添加工具栏

安卓 : Error when i set size of imageView inside TableRow programmatically

Android Studio - 首次在设备上运行应用程序时出现新遗留异常

javascript - 如何从 ListView 打开新 View

android - Genymotion - 在 etc/hosts 文件中设置哪个 IP 用于通过域访问主机的本地网络服务器?

Java Android - 在 ListView 中显示更多元素