0

ActionBarSherlockタブ内に配置したいので、正常に動作しているこのアクティビティをfragmentActivityに変換しようとしています。

アクティビティ:

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;
    }

}

どんな助けでも大歓迎です。

4

1 に答える 1

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)));
于 2013-05-28T18:38:33.327 に答える