如何以编程方式使用自定义库进度条?

时间:2015-07-27 16:04:22

标签: java android progress-bar customization

dependencies {
    compile 'com.github.navasmdc:MaterialDesign:1.5@aar'
}

我在项目中使用了这个材料设计库。

我希望在片段中以编程方式使用名为ProgressBarCircularIndeterminate的自定义进度条,如下所示。

RelativeLayout layout = (RelativeLayout)getActivity().findViewById(R.id.ll1);
        ProgressBarCircularIndeterminate progressBar = new ProgressBarCircularIndeterminate(getActivity(),null);

        progressBar.setVisibility(View.VISIBLE);
        layout.addView(progressBar);

但是我得到了例外。我的代码有什么错误吗? 请帮我解决问题。

请参阅:https://github.com/navasmdc/MaterialDesignLibrary/blob/master/MaterialDesign/src/com/gc/materialdesign/views/ProgressBarCircularIndeterminate.java

例外是

07-27 21:43:41.709  24071-24071/com.cook.chicken E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cook.chicken/com.cook.chicken.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4960)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.gc.materialdesign.views.ProgressBarCircularIndeterminate.setAttributes(ProgressBarCircularIndeterminate.java:37)
            at com.gc.materialdesign.views.ProgressBarCircularIndeterminate.<init>(ProgressBarCircularIndeterminate.java:25)
            at com.cook.chicken.HomeFragment.loadingPopup(HomeFragment.java:82)
            at com.cook.chicken.HomeFragment.onActivityCreated(HomeFragment.java:75)
            at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1797)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:979)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
            at android.app.Activity.performStart(Activity.java:5213)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2087)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4960)
            at java.lang.reflect.Method.invokeNative(Native Method)

1 个答案:

答案 0 :(得分:0)

第1步:

在Layout xml中添加Progressbar

last-of-type

第2步:

在您的活动课程中: 全局声明进度条。

<com.gc.materialdesign.views.ProgressBarCircularIndeterminate
            android:id="@+id/progressBarCircularIndeterminate"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:background="#1E88E5" />

onCreate()方法中:

     ProgressBarCircularIndeterminate progressBarCircularIndeterminate;

第3步:

为了启动计时器,你可以运行一个单独的线程,如下所示:

     int color = getIntent().getIntExtra("BACKGROUND", Color.BLACK);
     findViewById(R.id.progressBarCircularIndetermininate).setBackgroundColor(color);
     progressBarCircularIndeterminate = (ProgressBarCircularIndeterminate) findViewById(R.id. progressBarCircularIndeterminate);
     progressTimer.start();

注意:我刚刚分享了实施的步骤。希望您可以根据需要进行修改。