I have a custom Layout shown below and I would like to update the TextViews text from within that layout file. But the TextView is not within the layout file at all.
Sorry I don't know how to properly describe what I am implementing. If anyone could even advise on the correct terminology that would be much appreciated.
Basically I want to change the TextView from AM to PM when the button that is inflated from com.grogorian.android.control.MinutePicker is clicked. I am using the below java within com.grogorian.android.control.MinutePicker,but keep getting a null pointer
<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" />
Here is the Java
LinearLayout L = (LinearLayout)findViewById(R.id.L);
TextView Identifier = (TextView)L.findViewById(R.id.AMPMIdentifier);
Identifier.setText("PM");
EDIT: Here is the code from
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 );
If I were to guess, right now you're trying to find, in the OnCLickListener
listener, the LinearLayout
L from the layout file you posted(which you probably use as the layout for an Activity
?!). This will fail because the LinearLayout
will not be found and the object will be null
. If this what you are doing then try another approach:
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");
}
});
I haven't tested the code above(well, I don't even know how exactly your MinutePicker
is built). If this isn't the problem you may want to add the full exception stacktrace you may get.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.