setSupportProgressBarIndeterminateVisibility提高显示java.lang.NullPointerException了Android 5.0 SDK使用时(API 21) [英] setSupportProgressBarIndeterminateVisibility raising java.lang.NullPointerException when used with Android 5.0 SDK (API 21)

查看:421
本文介绍了setSupportProgressBarIndeterminateVisibility提高显示java.lang.NullPointerException了Android 5.0 SDK使用时(API 21)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动类扩展android.support.v7.app.ActionBarActivity。我请求窗口功能并在OnCreate()方法调用setSupportProgressBarIndeterminateVisibility()如下:

My activity class extends android.support.v7.app.ActionBarActivity. I am requesting window feature and calling setSupportProgressBarIndeterminateVisibility() in the onCreate() method as follows:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_main);
    setSupportProgressBarIndeterminateVisibility(true);
}

我无法领会错我的code和为什么我收到显示java.lang.NullPointerException 所提出的 setSupportProgressBarIndeterminateVisibility()

我的摇篮依赖性包含:

compile 'com.android.support:appcompat-v7:21.0.0'

有谁知道如何使用的support.v7操作栏中不确定的进度栏与API 21?

推荐答案

您需要使用工具栏,而不是动作条,并添加进度条到工具栏。

You need to use Toolbar instead of ActionBar and add the ProgressBar into the Toolbar.

下面是一个简单的解决方案,以插入不确定的进度条到工具栏;原来,这是一点都不难:)只要把你的进度XML元素工具栏里面是这样的:

Here is an easy solution to insert indeterminate ProgressBar into Toolbar; turns out it's not difficult at all :) Just put your ProgressBar xml element inside your Toolbar like this:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <ProgressBar
        android:id="@+id/progress_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:indeterminate="true"
        android:visibility="gone" />

</android.support.v7.widget.Toolbar>

然后在您的 AppCompatActivity ,检索工具栏后,只检索进度,并设置进度为可见或不可见,当你需要它。

And then in your AppCompatActivity, simply retrieve the ProgressBar after retrieving the Toolbar and set the ProgressBar to visible or invisible when you need it.

protected void onCreate(Bundle savedInstanceState) 
{
    setContentView(R.layout.toolbar);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    progressBar = (ProgressBar) findViewById(R.id.progress_spinner);

    //Make progress bar appear when you need it
    progressBar.setVisibility(View.VISIBLE);

    //Make progress bar disappear
    progressBar.setVisibility(View.INVISIBLE);
}

希望这有助于:)

Hope this helps :)

编辑:更换ActionBarActivity与AppCompatActivity根据最新的Andr​​oid支持库指南

replaced ActionBarActivity with AppCompatActivity as per the latest Android support libraries guidelines.

这篇关于setSupportProgressBarIndeterminateVisibility提高显示java.lang.NullPointerException了Android 5.0 SDK使用时(API 21)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆