2014-10-05 35 views
0

我正在嘗試使用mAltText在玻璃屏幕上顯示我的高度。當我調用卡類時,它給了我錯誤:「構造函數卡(new LocationHelper.LocationResult(){})未定義」,因爲我有「this」作爲參數。我應該使用什麼來代替「this」才能使其工作?我一整天都在搜索一個可行的例子,但無濟於事。Google Glass Card構造函數未定義

任何人都可以提供一些建議給新手||我應該使用android textview而不是卡片,然後將XML充填到視圖中?

LocationHelper是另一個.java文件中的另一個類。

public class MainActivity extends Activity { 
    private String mAltText; 
    LocationResult locationResult; 
    LocationHelper locationHelper; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     this.locationResult = new LocationResult(){ 
      @Override 
      public void gotLocation(Location location){ 

       if(location!=null){  
        double latitude = location.getLatitude(); 
        double longitude = location.getLongitude(); 
        double altitude = location.getAltitude(); 
        mAltText = (Double.toString(altitude)); 

        // display on card 
        Card card = new Card(this); //Error: The constructor Card(new LocationHelper.LocationResult(){}) is undefined 
        card.setText(mAltText); 
        View cardView = card.getView(); 
        setContentView(cardView); 

       }else{ 
        Log.e(TAG, "Location is null."); 
       } 
      } 
     }; 
     this.locationHelper = new LocationHelper(); 
     this.locationHelper.getLocation(this, this.locationResult); 
     } 
} 

回答

0

卡片構造函數採用上下文,你可以看到here

所以,你可以傳遞任何變量,它是Context的一個子類。就你而言,活動是一個不錯的選擇。

Card card = new Card(MainActivity.this); 

在代碼中,你不能只用this因爲this指LocationResult,它不是上下文。

Can anyone offer some advice to a newbie || should I use the android textview instead of card and then inflate an XML to the view?

您可以在卡片中顯示文本視圖。僅供參考,卡API由CardBuilder取代。如果您沒有任何特殊要求或自定義視圖,則可以使用CardBuilder。

+1

謝謝!這很好,我知道這很簡單。這源於我對「背景」究竟是什麼的有限瞭解。你救了我那天的朋友。 – Autex 2014-10-06 02:59:53

+0

@Autex非常受歡迎。快樂的編碼! – pt2121 2014-10-06 03:00:40