I am trying to implement the custom parameter ProgressBarPreference by adding the default preferences ProgressBar and TextView at the bottom and providing methods for the progress bar, such as setProgress () or setMax () to change it. Here my code works, besides updating progress in onCreate or onResume, for example, how can I let me set progress at these points? (this is actually a null pointer exception over mProgressBar):
ProgressBarPreference.java
public class ProgressBarPreference extends Preference { public ProgressBarPreference(Context context) { super(context); } public ProgressBarPreference(Context context, AttributeSet attrs) { super(context, attrs); } public ProgressBarPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } ProgressBar mProgressBar; @Override protected View onCreateView(ViewGroup parent) { LayoutInflater li = (LayoutInflater) Manager.appcontext.getSystemService(Service.LAYOUT_INFLATER_SERVICE); View myLayout=li.inflate(R.layout.progressbarpreference, null, false); ((ViewGroup)myLayout.findViewById(R.id.preference_super_container)).addView(super.onCreateView(parent)); mProgressBar=(ProgressBar) myLayout.findViewById(R.id.preference_progress_bar); return myLayout; } public void setProgress(int value){ mProgressBar.setProgress(value); } }
progressbarpreference.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/preference_super_container" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical" ></LinearLayout> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:paddingLeft="20sp" android:paddingRight="20sp" > <TextView android:layout_alignParentRight="true" android:text="0 new of 100" android:id="@+id/preference_progress_label" android:layout_height="wrap_content" android:layout_width="wrap_content" android:gravity="center" android:layout_centerVertical="true" android:paddingLeft="15sp" android:textColor="#FFFFFF" ></TextView> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/preference_progress_bar" android:layout_centerVertical="true" android:progress="50" android:layout_toLeftOf="@id/preference_progress_label" android:layout_alignParentLeft="true" ></ProgressBar> </RelativeLayout> </LinearLayout>
Addev source share