如何在android中的actionbar中设置自定义视图?

时间:2014-12-27 19:19:30

标签: android view android-actionbar android-custom-view

我想在操作栏应用程序标题中使用自定义字体,但每次我收到相同的错误(nullpointer异常),我的应用程序说我" Unfortunately **** has stopped"。我经常搜索并尝试使用它们。但是没有工作。同样的错误不断发生。我已经使用TextView创建了一个简单的布局,并尝试通过此行将其插入操作栏

ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.custom_actionbar);

无论我做什么,setCustomView都会向我发送nullpointer错误。我想如果我可以在操作栏中设置布局,那么很容易更改TextView字体。这是我的完整代码。

public class MainActivity extends ActionBarActivity {
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
        ActionBar actionBar = getActionBar();
        actionBar.setCustomView(R.layout.custom_actionbar);

    }
}

和布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" >

    <TextView
        android:id="@+id/App_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:textSize="40dp"
        android:maxLines="2"
        android:ellipsize="end"
        android:text="Prime" />
</RelativeLayout>

和错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tushar.primeplay2/com.example.tushar.primeplay2.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
            at android.app.ActivityThread.access$600(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5297)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.tushar.primeplay2.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5122)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
            at android.app.ActivityThread.access$600(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5297)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

我正在使用android studio。

2 个答案:

答案 0 :(得分:1)

试试这个

如果您使用支持库

ActionBar actionBar = getActionBar();

,否则

ActionBar actionBar = getSupportActionBar();

然后

LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(v);

您的onCreate应该是这样的

public class MainActivity extends ActionBarActivity {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
    ActionBar actionBar = getSupportActionBar(); // As you said you are using support library
    LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.custom_actionbar, null);
    actionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setCustomView(v);

}
}

记得导入

import android.support.v7.app.ActionBar

而不是

import android.app.ActionBar

答案 1 :(得分:-1)

 mSupportActionBar = getSupportActionBar();
 mSupportActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 mSupportActionBar.setCustomView (R.layout.action_bar_lt);