我正在尝试将此活动正常转换为fragmentActivity,因为我想将其放在ActionBarSherlock选项卡中。
的活动:
public class AndroidDetOrientationActivity extends Activity
implements SensorEventListener{
SensorManager sensorManager;
private Sensor sensorAccelerometer;
private Sensor sensorMagneticField;
private float[] valuesAccelerometer;
private float[] valuesMagneticField;
private float[] matrixR;
private float[] matrixI;
private float[] matrixValues;
TextView readingAzimuth, readingPitch, readingRoll;
Compass myCompass;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmenttwo);
readingAzimuth = (TextView)findViewById(R.id.azimuth);
myCompass = (Compass)findViewById(R.id.mycompass);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorMagneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
valuesAccelerometer = new float[3];
valuesMagneticField = new float[3];
matrixR = new float[9];
matrixI = new float[9];
matrixValues = new float[3];
}
@Override
protected void onResume() {
sensorManager.registerListener(this,
sensorAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this,
sensorMagneticField,
SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
}
@Override
protected void onPause() {
sensorManager.unregisterListener(this,
sensorAccelerometer);
sensorManager.unregisterListener(this,
sensorMagneticField);
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
switch(event.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
for(int i =0; i < 3; i++){
valuesAccelerometer[i] = event.values[i];
}
break;
case Sensor.TYPE_MAGNETIC_FIELD:
for(int i =0; i < 3; i++){
valuesMagneticField[i] = event.values[i];
}
break;
}
boolean success = SensorManager.getRotationMatrix(
matrixR,
matrixI,
valuesAccelerometer,
valuesMagneticField);
if(success){
SensorManager.getOrientation(matrixR, matrixValues);
int azimuth = (int) Math.toDegrees(matrixValues[0]);
readingAzimuth.setText("Degrees: " + String.valueOf(azimuth));
myCompass.update(matrixValues[0]);
}
}
}
片段活动:
public class FragmentTwo extends SherlockFragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragmenttwo, container, false);
return view;
}
}
非常感谢任何帮助。
答案 0 :(得分:0)
首先,对于ActionBarSherlock标签,您需要实施ActionBar.TabListener
扩展SherlockFragmentActivity,删除setContentView(R.layout.fragmenttwo);
您的FragmentActivity将从Fragment中扩展其布局。
然后添加Activity的onCreate():
ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab()
.setText("TAB 1")
.setTabListener(new TabListener<FragmentTwo>(this, "tab1", FragmentTwo.class)));