2012-11-09 36 views
0

我一直試圖從一個類中的方法傳遞一個字符串到另一個類中的方法,但似乎無法做到這一點。在類和方法之間傳遞字符串

我一直在嘗試了幾件事情像

String location = Latitude + "," + Longitude; 
AccelOrientExample GPSstring = new AccelOrientExample(); 
GPSstring.onSensorChanged(location); 

其中location是我想傳遞給類AccelOrientExample,在那裏我有與位置的arguement方法的字符串。

請幫幫忙,謝謝

無法實例類型AccelOrientExample是第一個錯誤,我得到這是對新的一面。而SensorEventListener類型中的onSensorChanged(SensorEvent)方法不適用於第二行的參數(字符串)。

public void onSensorChanged(SensorEvent sensorEvent, String location) 

是在AccelOrientExample

+2

你會得到什麼錯誤? *如何*這不起作用? – 2012-11-09 09:55:10

+0

你面臨的問題是什麼? –

+0

'似乎無法做到這一點'然後發生了什麼? –

回答

0

方法無法實例類型AccelOrientExample是第一個錯誤我 得到這是對新的一面。

我覺得你AccelOrientExample是一個抽象類。你不能用new操作符實例化你的Abstract類。你將不得不讓你的子類(具體類)初始化它。

like let assume AccelOrientedExampleImpl是AccelOrientedExample類的子類。

然後,

  AccelOrientExample GPSstring = new AccelOrientedExampleImpl(); 

的方法,在型 SensorEventListener onSensorChanged(SensorEvent)不適用於在 第二行中的參數(字符串)。

您的方法簽名需要兩個參數,SensorEvent實例和一個String。

public void onSensorChanged(SensorEvent sensorEvent, String location) 

您試圖僅傳遞String實例。您必須傳遞兩個參數,因爲您的方法需要兩個參數。做下面的事情,我假設SensorEvent是一個具體的類。

String location = Latitude + "," + Longitude; 
AccelOrientExample GPSstring = new AccelOrientExample(); 
GPSstring.onSensorChanged(new SensorEvent(), location); 
+0

另外,根據他發佈的異常,OP無法實例化'AccelOrientExample'。 –

+0

@RohitJain檢查我的編輯,我假設AccelOrientedExample是一個抽象類。請讓我知道我是否缺少其他東西) – PermGenError

+0

現在這是一個完整的答案。根據目前的問題,OP可以接受這個答案。 :) –