在操作栏中appcompat进度显示导致NPE [英] appcompat show progress in action bar causes NPE
问题描述
更新我的SDK所有最新的Android 5.0的好东西,我不能使用内置的appcompat的动作条进度条后。我已经做了所有常用的固定(移动supportRequestWindowFeature()调用setContent()之前在OnCreate中超级调用之前),但没有任何工程。下面是我在做什么:
After updating my SDK to all the latest Android 5.0 goodies I can't use progress bars built into the ActionBar in appcompat. I have done all the usual fixed (move supportRequestWindowFeature() call to before setContent() and before super call in oncreate) but nothing works. Here is what I'm doing:
public class LoginActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
...
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
setSupportProgressBarIndeterminateVisibility(true);
}
});
} 和堆栈跟踪:
} and the stack trace:
10-18 19:38:21.053: E/AndroidRuntime(11206): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.internal.widget.ProgressBarCompat.setVisibility(int)' on a null object reference
10-18 19:38:21.053: E/AndroidRuntime(11206): at android.support.v7.app.ActionBarActivityDelegateBase.updateProgressBars(ActionBarActivityDelegateBase.java:786)
10-18 19:38:21.053: E/AndroidRuntime(11206): at android.support.v7.app.ActionBarActivityDelegateBase.setSupportProgressBarIndeterminateVisibility(ActionBarActivityDelegateBase.java:692)
10-18 19:38:21.053: E/AndroidRuntime(11206): at android.support.v7.app.ActionBarActivity.setSupportProgressBarIndeterminateVisibility(ActionBarActivity.java:327)
10-18 19:38:21.053: E/AndroidRuntime(11206): at com.myapppackage.LoginActivity$2.onClick(LoginActivity.java:82)
这是一台Nexus 5运行Android 4.4.4。该应用程序的主题从Theme.AppCompat继承。这个应用程序是内置了Android 5.0和targetSDK为21。当我使用setSupportProgress一个正常的水平进度条同样的事情发生。任何帮助非常AP preciated。
This is on a Nexus 5 running Android 4.4.4. The app theme inherits from Theme.AppCompat. The app is built with Android 5.0 and targetSDK is 21. When I use setSupportProgress for a normal horizontal progress bar the same thing happens. Any help much appreciated.
编辑: 发现这个问题。在android.support.v7.internal.widget.ToolbarWidgetWrapper:
Found the problem. In android.support.v7.internal.widget.ToolbarWidgetWrapper:
@Override
public void initIndeterminateProgress() {
Log.i(TAG, "Progress display unsupported");
}
也许不是一个错误,但功能?工具栏似乎是新的其他动作。
Maybe not a bug but a feature? Toolbars seem to be the new ActionBars.
我在另一台计算机上,所以我要回的V20 appcompat的副本。
I have a copy of V20 appcompat on another computer so I'm going back to that.
推荐答案
一个可能的解决方法,这将是手动添加进度为自定义视图,并覆盖 setSupportProgressBarIndeterminateVisibility
A possible workaround for this would be to manually add a ProgressBar as a custom view and override setSupportProgressBarIndeterminateVisibility
在的onCreate:
In onCreate:
ProgressBar progressBar = new ProgressBar(this);
progressBar.setVisibility(View.GONE);
progressBar.setIndeterminate(true);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(progressBar);
在你的活动:
@Override
public void setSupportProgressBarIndeterminateVisibility(boolean visible) {
getSupportActionBar().getCustomView().setVisibility(visible ? View.VISIBLE : View.GONE);
}
当然,切换到工具栏,并添加进度应该有更多面向未来,中
Of course switching to a Toolbar and adding the ProgressBar there should be more future-proof.
这篇关于在操作栏中appcompat进度显示导致NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!