我有一个如下所示的自定义布局,我想从该布局文件中更新文本视图文本。但文本视图根本不在布局文件中。
抱歉,我不知道如何正确描述我正在实施的内容。如果有人能就正确的术语提供建议,那将不胜感激。
基本上,我想在单击从com.grogorian.android.control.MinutePicker膨胀的按钮时将TextView从AM更改为PM。我在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" />
这是爪哇
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 );
如果我猜的话,现在您正在尝试在OnCLickListener
侦听器中找到您发布的布局文件中的LinearLayout
L(您可能将其用作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
是如何构建的)。如果这不是问题所在,您可能需要添加可能获得的完整异常堆栈跟踪。