我是Android新手,所以我相信我在某個地方犯了一個錯誤,無法修復它。我有一個主要活動,讓我們稱之爲主菜單。從這我開始其他活動,按適當的按鈕啓動我想要的活動。問題是我在其中一個啓動的活動中使用了onSensorchanged()。如果應用程序第一次啓動,則啓動的活動值的行爲與預期相同,但如果關閉活動(換句話說,現在我回到主菜單中)並再次啓動活動,則會從onSensorchanged()讀取值跳躍。正確的值與錯誤的值結合在一起。我用意圖啓動第二項活動,並認爲問題在於此。onSensorChanged Azimut值跳
主菜單代碼:
public class MainMenuActivity extends Activity{
Button gps_btn,lineFollow_btn,man_control_btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
Debug.startMethodTracing("Navigation Trace");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
return true;
}
@Override
protected void onPause()
{
super.onPause();
Debug.stopMethodTracing();
}
public void addListenerOnButton()
{
final Context context = this;
gps_btn = (Button) findViewById(R.id.GPS_btn);
gps_btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent = new Intent(context, AutoControl.class);
startActivity(intent);
}
});
lineFollow_btn = (Button) findViewById(R.id.line_follower_btn);
lineFollow_btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent2 = new Intent(context, CamDemoActivity.class);
startActivity(intent2);
}
});
man_control_btn = (Button) findViewById(R.id.man_ctrl_btn);
man_control_btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent2 = new Intent(context, ManualAct_PLC.class);
startActivity(intent2);
}
});
}
的OnSensorChanged代碼:
public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
gravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
geoMagnetic = event.values;
if (gravity != null && geoMagnetic != null)
{
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, gravity,geoMagnetic);
if (success)
{
/* Orientation has azimuth, pitch and roll */
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
float azimut = orientation[0];
updateSensorInfoView(azimut);
steerRobot(azimut);
System.out.println("Raw Sensor value:" + Double.toString(Math.toDegrees(azimut)));
System.out.println("Degree Sensor value:" + calculations.compassDegString(azimut));
}
}
}
查看記錄的值,一旦自動控制活動又開始了我的傳感器數據是意外:
01-04 17:52:34.925: I/System.out(31387): Calculatebearing value:2.168326647225342
01-04 17:52:34.925: I/System.out(31387): Raw Sensor value:-116.11590698474976
01-04 17:52:34.925: I/System.out(31387): Degree Sensor value:244.0
01-04 17:52:34.955: I/System.out(31387): Calculatebearing value:173.96307878454928
01-04 17:52:34.955: I/System.out(31387): Raw Sensor value:72.0893408779263
01-04 17:52:34.955: I/System.out(31387): Degree Sensor value:72.0
01-04 17:52:35.065: I/System.out(31387): Calculatebearing value:108.5784565015826
01-04 17:52:35.065: I/System.out(31387): Raw Sensor value:137.473963160893
01-04 17:52:35.070: I/System.out(31387): Degree Sensor value:137.0
注意度數傳感器的值在短時間內已經大幅跳躍。基於計算出的方位角,我在屏幕上繪製了一個箭頭,一旦活動第二次加載,它就會像瘋狂一樣跳躍。
我懷疑這個活動創建了第二個實例,但在Manifest文件中使用了android:launchMode =「singleInstance」標籤來試圖防止這種情況發生。
我有其他人遇到過這樣的問題嗎?我似乎沒有在網上發現類似的問題。