我有一个带有片段对象的类;并且该片段对象包含几个搜索栏。
为了从片段中获取seekbar的值,我在main活动中实例化了fragmentObject并调用了fragmentObject.seekBar.getProgress();
然而;这不会返回什么?
主要活动代码:
//loops repeatedly
public void loop() throws ConnectionLostException, InterruptedException {
float f = ((float)fragmentObject.pwm1Seek.getProgress()/330);
}
}
相关片段代码:
package com.example.ioiorun;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.support.v4.app.Fragment;
public class fragmentExample extends Fragment {
String TAG;
// Declare all the UI Variables
View fragmentView;
SeekBar pwm1Seek;
public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
Bundle SavedInstantState) {
fragmentView = viewInflation.inflate(R.layout.pwmfragment_page,
container, false);
pwm1Seek = (SeekBar) pwmFragmentView.findViewById(R.id.pwm1SeekBar);
pwm1Seek.setMax(330);
pwm1Seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
float f = (float)progress/100;
pwm1Voltage.setText("Voltage: " + String.valueOf(f));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
_mainactivity.setCurrentPWMFragment(this);
return fragmentView;
}
public View getFragmentView() {
return fragmentView;
}
public void setFragmentView(View fragmentView) {
this.fragmentView = fragmentView;
}
}
答案 0 :(得分:0)
我发现了我的错误;我错误地在我的其他类中实例化我的片段对象。