2012-12-26 114 views
0

我從其他面臨同樣問題的用戶那裏讀了一些問題,但這些解決方案對我來說並不適用。 我在我的佈局xml定義的TextViewAndroid TextView setText不起作用

<TextView 
    android:id="@+id/currentLocation" 
    android:layout_column="0" 
    android:layout_row="14" 
    android:text="@string/currentLocation" /> 

現在在我的活動我得到

location = (TextView) findViewById(R.id.currentLocation); 

在我的活動TextView的,我也可以啓動其他兩項活動(一個是一個googlemap-View,另一個是手機的聯繫人列表)。我的主要活動onActivityResult()方法如下所示:

public void onActivityResult(int reqCode, int resultCode, Intent data) { 
    super.onActivityResult(reqCode, resultCode, data); 

    switch (reqCode) { 
     case (PICK_CONTACT) : 
      if (resultCode == Activity.RESULT_OK) { 
       Uri contactData = data.getData(); 
       Cursor c = managedQuery(contactData, null, null, null, null); 
       if (c.moveToFirst()) { 
        try{ 
         String address = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS)); 
         convertAddressToLatLon(address); 
        } 
        catch(Exception e){ 
         errorDialog.setMessage("Could not get Address. Try another contact or use Google Maps."); 
         errorDialog.show(); 
        } 

       } 
      } 
     case (PICK_GMAPS) : 
      if (resultCode == Activity.RESULT_OK) { 
       Bundle googleData = data.getExtras(); 
       location.setText(googleData.getString("address")); 
      } 
     break; 
    } 
} 

convertAddressToLatLon(address)方法也有一個location.setText();與definatelly一個stringValue的!

所以,如果我從googlemap-activity返回,位置文本更改爲給定的輸入。在接觸活動部分它不起作用。但沒有錯誤。它到達location.setText()方法,但不會更改文本。我不知道什麼是錯的。它不可能,字符串中存在錯誤的字符。即使我在catch塊的閉合框架後放置'location.setText(「test」)',它也不起作用。 (再次,程序進入if(c.moveToFirst()){部分!)。我不知道什麼是錯的,因爲就像我說的googlepart工作正確。

+0

其中是你的textView的layout_width和高度參數? –

+0

@JimPanse爲什麼你不設置textview的高度和寬度? –

+0

您是否嘗試記錄該值? – adarsh

回答

2

發現錯誤。我忘了放一個「休息」。在PICK_CONTACT案例結束時。現在它的工作...

1

至少設置您的TextView的高度和寬度。
某事像:

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 

我覺得這些都是基本參數。那麼你可以尋求其他原因(如果錯誤繼續)。

+0

即使我忘了這些參數,谷歌地圖部分作品,聯繫人部分不... –