动作条给予NullPointerException异常? [英] ActionBar giving nullPointerException?

查看:166
本文介绍了动作条给予NullPointerException异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出这一个了一段时间。我有一个使用操作栏,但我想它调用每次出现空的应用程序。给这个误差

  10月12日至1日:54:11.499:E / AndroidRuntime(2385):致命异常:主要
10月12日至1日:54:11.499:E / AndroidRuntime(2385):工艺:com.example.jobpool,PID:2385
10月12日至1日:54:11.499:E / AndroidRuntime(2385):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.jobpool / com.example.jobpool.Signup}:显示java.lang.NullPointerException:尝试调用虚拟方法无效android.app.ActionBar.hide()在空对象引用
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.os.Handler.dispatchMessage(Handler.java:102)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.os.Looper.loop(Looper.java:135)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.app.ActivityThread.main(ActivityThread.java:5221)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在java.lang.reflect.Method.invoke(本机方法)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在java.lang.reflect.Method.invoke(Method.java:372)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):显示java.lang.NullPointerException:产生的原因试图调用虚拟方法无效android.app.ActionBar.hide()在空对象引用
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在com.example.jobpool.Signup.onCreate(Signup.java:16)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.app.Activity.performCreate(Activity.java:5933)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
10月12日至1日:54:11.499:E / AndroidRuntime(2385):10 ...更多

我知道这在其他线程的讨论,但我还没有发现,我的解决方案。我有app.support.v7库补充说。

下面是onCreate方法:

 公共类注册扩展活动
{
      保护无效的onCreate(捆绑savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.signup);
            getActionBar()隐藏()。
            SetListeners();
        }....

下面是清单:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.jobpool
    安卓版code =1
    机器人:=的versionName1.0>
    机器人:可调试=真
    机器人:VMsafemode =真
    机器人:AllowBackup =真
    机器人:AllowClearuserdate =真
    机器人:HardwareAccelerated =真
    <使用许可权的android:NAME =android.permission.INTERNET对/>
    <使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>    <用途-SDK
        安卓的minSdkVersion =11
        机器人:targetSdkVersion =21/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ logo_icon_large
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=注册
            机器人:标签=注册>
        < /活性GT;
        <活动
            机器人:仪表盘NAME =
            机器人:标签=仪表板>
        < /活性GT;
        <活动
            机器人:UserRegistrationNAME =
            机器人:标签=@字符串/ title_activity_user_registration>
        < /活性GT;
    < /用途>< /清单>

和最后我的价值观/风格文件

 <资源>    <! - 
        基础应用的主题,依赖于API级别。这个主题被替换
        通过AppBaseTheme从RES /值-VXX /较新设备上styles.xml。
     - >
    <样式名称=AppBaseTheme父=@风格/ Theme.AppCompat.Light.DarkActionBar>
        <! -
            在新API的级别可以去提供自定义主题
            RES /值-VXX / styles.xml,而自定义相关
            向后兼容性可以去这里。
         - >
    < /风格>    <! - 应用的主题。 - >
    <样式名称=AppTheme父=AppBaseTheme>
        <! - 所有自定义不特定于一个特定的API级别的可以去这里。 - >
          <项目名称=机器人:windowNoTitle>真< /项目> <! - 隐藏操作栏 - >
          <项目名称=机器人:windowFullscreen>真< /项目> <! - 隐藏状态栏 - >
    < /风格>< /资源>


解决方案

如果您使用的是支持库,那么你必须使用 getSupportActionBar()而不是 getActionBar(),而是延伸活动你必须扩展 ActionBarActivity 这里你可以找到的文档

I've been trying to figure this one out for a while. I have an app that uses the action bar but every time I want to call it in comes up null. Giving this error

    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

I know this has been discussed in other threads but I haven't found a solution that works for me. I have the app.support.v7 library added.

Here's the onCreate method:

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

....

Here's the manifest:

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

and lastly my values/styles file

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

解决方案

if you are using the support library then you have to use getSupportActionBar() instead of getActionBar(), and instead of extends Activity you have to extends ActionBarActivity. Here you can find the documentation

这篇关于动作条给予NullPointerException异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆