0

我想在我的应用程序中使用 ActionBarSherlock。当我打电话时,getSupportActionBar()我得到一个NullPointerException;我已经将 ActionBarSherlock 库导入到我的项目中。

这是我的Java类:

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


              }
        }

}

还有我的清单文件:

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

谢谢。

这是我的风格:

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

这里是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
4

2 回答 2

1

我现在无法解释您为什么要获得 NPE,但请尝试以下操作:

而不是使用

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

就用这个

  setTitle("Example!");
于 2013-03-24T16:47:50.110 回答
1

您的应用程序主题是否扩展了 ABS 主题?

例如,当您指定:

android:theme="@style/AppTheme"

你的风格应该是:

<style name="AppTheme" parent="Theme.Sherlock.Light">
    ...
</style>

另请参阅Jake Wharton 本人的这个答案

于 2013-03-24T16:14:14.833 回答