其他分享
首页 > 其他分享> > 如何在Android的动作栏中设置自定义视图?

如何在Android的动作栏中设置自定义视图?

作者:互联网

我想在动作栏应用程序标题中使用自定义字体,但是每次遇到相同的错误(nullpointer异常)时,我的应用程序都会告诉我“不幸的是****已停止”.我进行了很多搜索并尝试使用它们.但是什么都没有.相同的错误不断发生.我使用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.

解决方法:

尝试这个

如果您正在使用支持库

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

标签:android,android-actionbar,view,android-custom-view
来源: https://codeday.me/bug/20191013/1906643.html