2012-05-06 58 views
0

我有一個自定義佈局如下所示,我想更新該佈局文件中的TextViews文本。但是TextView根本不在佈局文件中。自定義佈局與其他佈局交互TextView

對不起,我不知道如何正確描述我正在執行的內容。如果任何人甚至可以建議正確的術語,將不勝感激。

基本上我想將TextView從AM更改爲PM,當從com.grogorian.android.control.MinutePicker充氣的按鈕被點擊時。我使用內com.grogorian.android.control.MinutePicker下面的Java,但要得到一個空指針

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/L" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"> 
<com.grogorian.android.control.MinutePicker 
     android:id="@+id/Picker2" 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </com.grogorian.android.control.MinutePicker> 
    <TextView android:id="@+id/AMPMIdentifier" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="AM" /> 

這裏是Java的

LinearLayout L = (LinearLayout)findViewById(R.id.L); 
    TextView Identifier = (TextView)L.findViewById(R.id.AMPMIdentifier); 
    Identifier.setText("PM"); 

編輯:這裏是代碼從

but = new Button(context); 
    but.setTextSize(TEXT_SIZE); 
    but.setText("-"); 

    // Decrement once for a click 
    but.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
LinearLayout L = (LinearLayout)findViewById(R.id.L); 
    TextView Identifier = (TextView)L.findViewById(R.id.AMPMIdentifier); 
    Identifier.setText("PM"); 
     } 
    }); 
      this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    LayoutParams elementParams = new LinearLayout.LayoutParams(ELEMENT_WIDTH, ELEMENT_HEIGHT); 

     addView(but, elementParams); 
+0

沒有足夠的信息。顯示你設置你的佈局的類以及com.grogorian.android.control.MinutePicker ...,這會給人一個戰鬥機會來幫助你。 – Barak

+0

添加了所需的代碼。謝謝 – Somk

+0

我沒有在任何地方看到一個'setContentView' ... – Barak

回答

0

如果我猜測,現在你正試圖找到,在OnCLickListener聽衆,LinearLayoutL從您發佈的佈局文件(您可能用作Activity?的佈局)。這將失敗,因爲將找不到LinearLayout,對象將爲null。如果你在做什麼,然後再嘗試另一種方法:

but.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     LinearLayout parent = (LinearLayout) v.getParent(); // I assumed your MinutePicker extends LinearLayout 
     LinearLayout L = (LinearLayout) parent.getParent(); 
     TextView Identifier = (TextView)L.findViewById(R.id.AMPMIdentifier); 
     Identifier.setText("PM"); 
    } 
}); 

我沒有測試上面的代碼(好,我甚至不知道你的MinutePicker究竟是建立)。如果這不是問題,則可能需要添加可能獲得的完整異常堆棧跟蹤。

+0

謝謝你工作完美 – Somk