ProgressBar上的NPE

时间:2017-01-28 19:42:14

标签: java android

每次我想要更改进度条的可见性时,我都有NullPointerException。它正在使用任何其他布局项目,但这。我也有两个文本视图以相同的方式初始化,但它们正在工作。你知道这是什么问题吗?

片段代码:

@BindView(R.id.progress_indicator) ProgressBar mProgressBar;

@Override
public void setLoadingIndicator(boolean active) {
    if (getView() == null) {
        return;
    }

    if(active) {
        mProgressBar.setVisibility(View.VISIBLE);
    } else {
        mProgressBar.setVisibility(View.INVISIBLE);
    }
}

布局:

<ProgressBar
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progress_indicator"
    android:visibility="visible"
    android:layout_marginTop="32dp"
    app:layout_constraintTop_toBottomOf="@+id/thisWeekOnDuty"
    android:layout_marginEnd="152dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="152dp"
    android:indeterminate="true" />

堆栈跟踪:

E/UncaughtException: java.lang.RuntimeException: Unable to resume activity {pl.rasztabiga.klasa1a/pl.rasztabiga.klasa1a.mainAct.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
                                                                        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3444)
                                                                        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3484)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2747)
                                                                        at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6176)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
                                                                        at pl.rasztabiga.klasa1a.mainAct.OnDutiesFragment.setLoadingIndicator(OnDutiesFragment.java:84)
                                                                        at pl.rasztabiga.klasa1a.mainAct.OnDutiesPresenter.onCreateLoader(OnDutiesPresenter.java:51)
                                                                        at android.support.v4.app.LoaderManagerImpl.createLoader(LoaderManager.java:539)
                                                                        at android.support.v4.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:548)
                                                                        at android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:603)
                                                                        at pl.rasztabiga.klasa1a.mainAct.OnDutiesPresenter.start(OnDutiesPresenter.java:46)
                                                                        at pl.rasztabiga.klasa1a.mainAct.OnDutiesFragment.onResume(OnDutiesFragment.java:50)
                                                                        at android.support.v4.app.Fragment.performResume(Fragment.java:2133)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1156)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1277)
                                                                        at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2164)
                                                                        at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:223)
                                                                        at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:509)
                                                                        at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:498)
                                                                        at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:175)
                                                                        at android.app.Activity.performResume(Activity.java:6807)
                                                                        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3421)
                                                                        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3484) 
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2747) 
                                                                        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:154) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6176) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

2 个答案:

答案 0 :(得分:1)

尝试在fragment中绑定您的视图:

 @Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.your_fragment, container, false);
        ButterKnife.bind(this, view);
        // TODO Use fields...
        return view;
      }
}

请参阅此http://jakewharton.github.io/butterknife/了解非活动绑定

答案 1 :(得分:0)

您是否在活动中致电ButterKnife.bind(this); onCreate?

在能够正确调试之前,我需要更多信息。