我正在尝试以下示例代码。即使我的文本视图以类似的方式更新。在seekbar.setProgress(45)它抛出一个空指针异常,好像它还没有找到了搜索栏..任何想法为什么?所有textview和seekbar都以类似的方式在onCreateView和onStart中初始化。除了那个搜索栏监听器被称为适当的ontouch拖动等..
public class HomeMenuFragment extends Fragment {
TextView tvHomeMenuText;
TextView tvHomeMenuTextUnit;
ImageButton btnHomeMenu;
public SeekBar menuSeek;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_home, container, false);
menuSeek = (SeekBar) v.findViewById(R.id.sbHomeMenu);
tvHomeMenuText = (TextView) v.findViewById(R.id.tvHomeMenuText);
tvHomeMenuTextUnit = (TextView) v.findViewById(R.id.tvHomeManuTextUnit);
btnHomeMenu = (ImageButton) v.findViewById(R.id.btnHomeMenu);
return v;
}
@Override
public void onStart() {
super.onStart();
/* Set Typeface */
AssetManager am = getActivity().getAssets();
Typeface typeface = Typeface.createFromAsset(am, String.format(Locale.US, "fonts/%s", "century_gothic_bold.ttf"));
tvHomeMenuText.setTypeface(typeface);
tvHomeMenuTextUnit.setTypeface(typeface);
menuSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int translatedProgress;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
translatedProgress = progress + 60;
tvHomeMenuText.setText(Integer.toString(translatedProgress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
tvHomeMenuText.setText(Integer.toString(translatedProgress));
}
});
menuSeek.setProgress(75);
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
tvHomeMenuText.setText("--");
tvHomeMenuTextUnit.setText("--");
menuSeek.setProgress(45);
// upadte textView here
handler.postDelayed(this, 500); // set time here to refresh textView
}
});
}
@Override
public void onResume() {
super.onResume();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}