动作条的NullPointerException [英] ActionBar nullpointerException

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

问题描述

我要与我的应用程序使用ActionBarSherlock。当我打电话 getSupportActionBar()我收到了 NullPointerException异常;我已经导入ActionBarSherlock库到我的项目。

下面是我的Java类:

 公共类ProfileActivity扩展SherlockActivity {
    @覆盖
            公共布尔onCreateOptionsMenu(com.actionbarsherlock.view.Menu菜单){
                com.actionbarsherlock.view.MenuInflater吹气= this.getSupportMenuInflater();
                inflater.inflate(R.menu.mainmenu,菜单);
                返回true;
            }        公共无效的onCreate(捆绑savedInstanceState){
                super.onCreate(savedInstanceState);                。getWindow()requestFeature(Window.FEATURE_ACTION_BAR);                的setContentView(R.layout.profile_page);                getSupportActionBar()的setTitle(示例!)。 //空指针异常
              }
        }}

和我的清单文件:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.sal
    安卓版code =1
    机器人:=的versionName1.0>    <用途特征
        机器人:glEsVersion =0x00020000
        机器人:要求=真/>    <用途-SDK
        安卓的minSdkVersion =11
        机器人:targetSdkVersion =10/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>
    <使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>
    <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <使用许可权的android:NAME =android.permission.CAMERA/>    <许可
        机器人:名字=com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE
        安卓的ProtectionLevel =签名/>    <使用许可权的android:NAME =com.google.android.providers.gsf.permission.READ_GSERVICES/>
    <! - 不需要下面的两个权限使用
     谷歌地图Android版API V2,但建议。 - >
    <使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>
        <使用许可权的android:NAME =com.example.sal.permission.MAPS_RECEIVE/>
        <使用许可权的android:NAME =android.permission.READ_PHONE_STATE/>
    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme
        机器人:名字=FriendPickerApplication。>        <活动
            机器人:名字=com.example.sal.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
              < /活性GT;         <! - 入境RegisterActivity.class - >
        <活动机器人:名字=。RegisterActivity
                  机器人:标签=注册新帐号>< /活性GT;
        <元数据的android:值=@字符串/ APP_ID机器人:名字=com.facebook.sdk.ApplicationId/>
        <活动机器人:标签=@字符串/ APP_NAME机器人:名字=com.facebook.LoginActivity>< /活性GT;
        <活动机器人:ProfileActivityNAME =机器人:标签=个人资料页>< /活性GT;        &所述;元数据
              机器人:名字=com.google.android.maps.v2.API_KEY
              机器人:值=AIzaSyDTVRXMTmfe3jZyAlt-87kRgqM8y4TzIto/>
        <活动机器人:名字=SaleMapActivity>< /活性GT;
        <活动机器人:名字=GroupMapActivity>< /活性GT;
        <活动机器人:名字=DeliverMapActivity>< /活性GT;
        <活动机器人:名字=PickFriendsActivity>< /活性GT;
        <活动机器人:名字=TabMainActivity>< /活性GT;
        <活动机器人:名字=itemSaleActivity>< /活性GT;
        <活动机器人:名字=FreindsActivity>< /活性GT;
        <活动机器人:名字=ExitActivity>< /活性GT;
        <活动机器人:名字=OneTimeDetailesScreen>< /活性GT;    < /用途>< /清单>

感谢。

这就是我的风格:

 <样式名称=AppTheme父=Theme.Sherlock.Light>
        <项目名称=机器人:windowActionBar>真< /项目>
    < /风格>

ADN这里是logcat的:

  03-24 16:20:24.325:E / AndroidRuntime(1927年):显示java.lang.NullPointerException:产生的原因
03-24 16:20:24.325:E / AndroidRuntime(1927年):在com.example.sal.ProfileActivity.onCreate(ProfileActivity.java:349)
03-24 16:20:24.325:E / AndroidRuntime(1927年):在android.app.Activity.performCreate(Activity.java:5104)
03-24 16:20:24.325:E / AndroidRuntime(1927年):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-24 16:20:24.325:E / AndroidRuntime(1927年):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-24 16:20:24.325:E / AndroidRuntime(1927):23 ...更多


解决方案

我现在不能解释为什么你所得到的NPE,但请试试这个:

而不是使用

  getSupportActionBar()的setTitle(示例!)。 //空指针异常

只是使用

 的setTitle(例子!);

I want to use ActionBarSherlock with my app. When I call getSupportActionBar() I get a NullPointerException; I already imported the ActionBarSherlock library into my project.

Here is my Java class:

public class ProfileActivity extends SherlockActivity  {


    @Override
            public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
                com.actionbarsherlock.view.MenuInflater inflater = this.getSupportMenuInflater();
                inflater.inflate(R.menu.mainmenu, menu);
                return true;
            }

        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

                setContentView(R.layout.profile_page);



                getSupportActionBar().setTitle("Example!");  //nullpointerException


              }
        }

}

And my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sal"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>



    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <permission
        android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="com.example.sal.permission.MAPS_RECEIVE"/>
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>




    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:name=".FriendPickerApplication">



        <activity
            android:name="com.example.sal.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>

         <!--  Entry for RegisterActivity.class -->
        <activity android:name=".RegisterActivity"
                  android:label="Register New Account"></activity>
        <meta-data android:value="@string/app_id" android:name="com.facebook.sdk.ApplicationId"/>
        <activity android:label="@string/app_name" android:name="com.facebook.LoginActivity"></activity>
        <activity android:name=".ProfileActivity" android:label="Profile page"></activity>

        <meta-data
              android:name="com.google.android.maps.v2.API_KEY"
              android:value="AIzaSyDTVRXMTmfe3jZyAlt-87kRgqM8y4TzIto"/>


        <activity android:name="SaleMapActivity"></activity>
        <activity android:name="GroupMapActivity"></activity>
        <activity android:name="DeliverMapActivity"></activity>
        <activity android:name="PickFriendsActivity"></activity>
        <activity android:name="TabMainActivity"></activity>
        <activity android:name="itemSaleActivity"></activity>
        <activity android:name="FreindsActivity"></activity>
        <activity android:name="ExitActivity"></activity>
        <activity android:name="OneTimeDetailesScreen"></activity>

    </application>

</manifest>

Thanks.

and this is my style:

<style name="AppTheme" parent="Theme.Sherlock.Light">
        <item name="android:windowActionBar">true</item>
    </style>

adn here is the logcat:

03-24 16:20:24.325: E/AndroidRuntime(1927): Caused by: java.lang.NullPointerException
03-24 16:20:24.325: E/AndroidRuntime(1927):     at com.example.sal.ProfileActivity.onCreate(ProfileActivity.java:349)
03-24 16:20:24.325: E/AndroidRuntime(1927):     at android.app.Activity.performCreate(Activity.java:5104)
03-24 16:20:24.325: E/AndroidRuntime(1927):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-24 16:20:24.325: E/AndroidRuntime(1927):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-24 16:20:24.325: E/AndroidRuntime(1927):     ... 23 more

解决方案

I cannot now explain why you are getting the NPE but please try this:

Instead of using

getSupportActionBar().setTitle("Example!");  //nullpointerException

just use this

  setTitle("Example!");

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

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