2013-05-28 129 views
0

我試圖將這個工作正常的活動轉換爲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

首先對ActionBarSherlock選項卡,你需要實現ActionBar.TabListener

延長SherlockFragmentActivity,刪除setContentView(R.layout.fragmenttwo);您FragmentActivity將膨脹從尾數其佈局。 然後加在你活動的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))); 
+0

是的,我有TabListner,我現在有4個標籤,他們正在工作的罰款。我試圖將該指南針活動轉換爲片段,因此我可以將它放在Tab 2中。我想知道這是否可能,或者嘗試啓動片段內的指南針活動。謝謝。 – user2122179

+0

@ user2122179請參閱最新的答案。 – mt0s