2013-10-24 16 views
1

我有一個自定義組件(像信息面板) - LinearLayout中很少textviews的。 我正在使用android註釋。
Android的註解不知道TextView的

問題
如果我嘗試使用@AfterViews,則永遠不會調用該方法(問題1)。
問題是當我嘗試設置在東西通過註釋TextView的,即TextView的爲空(問題2)..

,如果我嘗試從另一個類,在那裏我相信Android的註釋工作,我可以很容易並沒有任何問題得到該textView和設置那裏的東西..該變量不是空的,並沒有問題..但在另一個類中,我使用AA,我也使用活動以及..:/所以我想如果有在@EViewGroup問題。但根據自己的javadoc,@EVIewGroup與AfterViews和ViewById沒有問題。(根據自己的網站,快速一切的ViewGroup:],我想我選擇從LinearLayout中延長右可能性)

有人有一些小費我失蹤了嗎?

- 我用一個更片段自定義組件。
- 有在這個類一類的調用方法( - >代碼預覽 - )somethingIsCalling(),如果有些地方想在TextView中顯示)

問題代碼,方法燒製:

定製組件的佈局(info_panel.xml):

:針對自定義組件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:baselineAligned="false" 
    android:id="@+id/infoPanel"> 
    <TextView 
     android:id="@+id/txtAccName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:gravity="right|center_vertical" /> 
</LinearLayout> 

java代碼

+0

您是否在使用@EActivity註釋的活動中使用該自定義視圖?否則它將無法工作。 – marsbear

回答

4

這裏是我會重寫你的代碼來得到它的工作:

layout_info_panel.xml

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android"> 
    <TextView 
     android:id="@+id/txtAccName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="right|center_vertical" /> 
</merge> 

注意如何通過<merge/>取代根<LinearLayout/>在這裏,但仍然能夠使用所有<LinearLayout/>特定的屬性(如layout_weight="1")(https://github.com/excilys/androidannotations/wiki/Enhance-custom-views#how-to-create-it-
這可能是因爲您的自定義class InfoPanel extends LinearLayout
作爲獎勵,你可以刪除1級層次的,這始終是小的性能提升(http://www.curious-creature.org/2012/12/01/android-performance-case-study/

InfoPanel.java

@EViewGroup(R.layout.info_panel) 
public class InfoPanel extends LinearLayout { 
    @ViewById(R.id.txtAccName) 
    protected TextView txtName; 

    public InfoPanel(Context ctx, AttributeSet attr) { 
     super(ctx, attr); 
     // remove this inflation, AndroidAnnotations does this for you in @EViewGroup(R.layout.info_panel) 
     // LayoutInflater.from(ctx).inflate(R.layout.info_panel, this); 
    } 

    @AfterViews 
    protected void init() { 
      // PROBLEM 1 would be fine now 
     txtName.setText("call after view"); 
    } 

    public void somethingIsCalling() { 
      // PROBLEM 2 also will be resolved IF you call it after @AfterViews method has done binding. 
      txtName.setText("something is calling"); 
    } 
}

現在,無論你想在XML的信息面板,請確認到包括生成的類(一個與_underscore),否則你的領域將確實總是爲空,即

編輯:顯然在這種情況下這是造成問題的OP和改變<com.stackoverflow.answers.InfoPanel><com.stackoverflow.answers.InfoPanel_>解決的領域是null

layout_ordinary_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <com.stackoverflow.answers.InfoPanel_ 
     android:id="@+id/infoPanelView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    … 
</LinearLayout> 

https://github.com/excilys/androidannotations/wiki/Enhance-custom-views#how-to-use-it-
我建議你去通過所有食譜中AndroidAnnotations維基,有對夫婦更常見的錯誤在那裏。

+0

我終於找到了答案..有點別的然後你..但在你的幫助有一個答案:]問題是與使用該面板..我有它像而不是 ..我知道我需要在任何地方使用InfoPanel_,但是我並不認爲我需要在XML中使用它。如果你修改了一點點,你是最接近的強調答案2)我會將它標記爲答案。謝謝! – piggy

+0

這個鬼鬼祟祟的下劃線... +1提供了一個易於檢測的廣泛列表。 – dbm

0

嘗試刪除從@EViewGroup的說法,它已經解決了simiilar問題對我來說。

+0

它應該如何解決問題,如果你想使用@ViewById你需要使用像EViewGroup?另一個放在那裏? – piggy

+0

您仍然使用@EViewGroup,但它的參數是可選的,並且您不需要它,因爲您已經在LayoutInflater.from(ctx).inflate(R.layout.info_panel,this)中設置了佈局; –

+0

是的,但如果我沒有那裏的參數,我得到的錯誤,「@ViewById」只能用於「@EViewGroup,@EGroup,@EActivity」等。 – piggy