隐藏操作栏在Android活动中显示Nullpointerexception

时间:2015-04-06 06:35:33

标签: android nullpointerexception android-actionbar fading

我试图在我的android活动中添加淡入淡出的操作栏,但它显示空指针异常。下面我添加了主题,风格,清单和一切。

这是我的代码

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.manuelpeinado.fadingactionbar.FadingActionBarHelper;
import com.xxx.xxx.R;
import com.xxx.xxx.cache.ImageLoader;
import com.xxx.xxx.header.Header;

public class List extends Activity
{
    ListView listView;
    private MyAdapter adapter;
    Context context;
    public static int click_position;
    public static String name="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FadingActionBarHelper helper = new FadingActionBarHelper()
        .actionBarBackground(R.drawable.ab_background)
        .headerLayout(R.layout.header)
        .contentLayout(R.layout.list);
        setContentView(helper.createView(this));
        helper.initActionBar(this);

的manifest.xml

       <activity
            android:name=".List"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait"
            android:icon="@drawable/icon"
            android:label="List"
            android:theme="@style/AppTheme.TranslucentActionBar" >
        </activity>

styles.xml

<resources>
    <style name="Widget" />

    <style name="Widget.ActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse" />

    <style name="Widget.Light" />

    <style name="Widget.Light.ActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid" />

    <style name="Widget.ActionBar.Transparent">
        <item name="android:background">@android:color/transparent</item>
    </style>

    <style name="Widget.Light.ActionBar.Transparent">
        <item name="android:background">@android:color/transparent</item>
    </style>

</resources>

的themes.xml

<resources>

    <style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/Widget.ActionBar</item>
    </style>

    <style name="AppTheme.TranslucentActionBar">
        <item name="android:actionBarStyle">@style/Widget.ActionBar.Transparent</item>
        <item name="android:windowActionBarOverlay">true</item>
    </style>

    <style name="AppTheme.Light.TranslucentActionBar" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/Widget.Light.ActionBar.Transparent</item>
        <item name="android:windowActionBarOverlay">true</item>
    </style>

</resources>

这是我的日志:

04-06 12:00:55.860: E/AndroidRuntime(23504): Caused by: java.lang.NullPointerException
04-06 12:00:55.860: E/AndroidRuntime(23504):    at com.xxx.xxx.List.onCreate(List.java:35)

1 个答案:

答案 0 :(得分:0)

你的java代码不完整......没有包含ActionBar。 所以你需要一个用方法声明的actionBar:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Create menu display from the layout XML file menu_main.xml
    MenuInflater inflater = getMenuInflater();
    inflater.inflate( R.menu.menu , menu );
    //Get actionbar as object
    ActionBar actionbar = getSupportActionBar();

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
//define action bar action here
return true;
}

然后,如果您使用的是android.support.v7.app.ActionBar,则需要更改include

import com.manuelpeinado.fadingactionbar.FadingActionBarHelper;

通过

import com.manuelpeinado.fadingactionbar.extras.actionbarcompat.FadingActionBarHelper;