很简单,我声明了ActionBar,调用hide()并得到了一个nullpointer异常。这是一个常见问题,所以我将确保我提及自己尝试过的内容。

-确保AppTheme没有声明Theme.WithoutActionBar

-使用import android.support.v7.app.ActionBarActivity;扩展ActionBarActivity而不是Activity;

-确保我使用的最小SDK为> 11

编辑:当我将项目切换到Android Studio时,ActionBar返回null的整个混乱开始了

这是我在oncreate()里面的代码,在这里我弄乱了操作栏。

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);
    ActionBar bar = getActionBar();
    bar.hide();


这是我的logcat错误

Process: com.example.rafa.ideas, PID: 19806
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rafa.ideas/com.example.rafa.ideas.LoginActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
        at android.app.ActivityThread.access$900(ActivityThread.java:175)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at com.example.rafa.ideas.LoginActivity.onCreate(LoginActivity.java:33)
        at android.app.Activity.performCreate(Activity.java:5451)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)


编辑:这是我的清单
    


<application
    android:name=".IdeasApplication"
    android:allowBackup="true"
    android:icon="@drawable/idea_logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".SignupActivity"
        android:label="@string/app_name"
        android:parentActivityName=".LoginActivity" >
    </activity>
    <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=".LoginActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize|stateHidden" >
    </activity>
    <activity
        android:name=".AddIdeaActivity"
        android:label="@string/app_name" android:parentActivityName=".MainActivity">
    </activity>
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>


编辑:这是我在样式设置中的主题
    

<!--
    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="Theme.AppCompat.Light">
    <!--
        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. -->
</style>

最佳答案

如果正在使用getSupportActionBar,则应使用getActionBar而不是ActionBarActivitygetActionBar方法在ActionBarActivity API中可用,因为它扩展了Activity,但在使用支持活动时不应使用。

关于java - ActionBar返回NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27663172/

10-09 12:57