简体   繁体   中英

TextView method setText() not working in method onClick()

there. I am a new Android developer, and my experience is still quite lacking. I'm trying to update the GUI by having a log that updates itself every time a button is clicked. My question is: Why don't the "log#" TextViews update with setText() when the "p#" TextViews will? My code is as follows:

public class ZARP extends Activity
{

    ..
    private void b(P p, E)
    {


        setContentView(R.layout.b);

        TextView p1 = (TextView)findViewById(R.id.textView1);
        TextView p2 = (TextView)findViewById(R.id.textView2);
        TextView p3 = (TextView)findViewById(R.id.textView3);
        TextView p4 = (TextView)findViewById(R.id.textView4);
        TextView p5 = (TextView)findViewById(R.id.textView5);
        TextView p6 = (TextView)findViewById(R.id.textView6);

        p1.setText("Hello");//WORKS
        p2.setText("Hello");//WORKS
        p3.setText("Hello");//WORKS
        p4.setText("Hello");//WORKS
        p5.setText("Hello");//WORKS
        p6.setText("Hello");//WORKS
    ..


        Button f = (Button)findViewById(R.id.button2);



        f.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
                {          

                    setContentView(R.layout.b);
                    TextView log1 = (TextView)findViewById(R.id.textView7);
                    TextView log2 = (TextView)findViewById(R.id.textView8);
                    TextView log3 = (TextView)findViewById(R.id.textView9);
                    TextView log4 = (TextView)findViewById(R.id.textView10);




                    log4.setText(log3.getText());//DOESN'T WORK
                        log3.setText(log2.getText());//DOESN'T WORK             
                    log2.setText(log1.getText());//DOESN'T WORK
                    log1.setText("Hello");//DOESN'T WORK

    }
...

}

And the XML for the b.XML is as follows:

<TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tableRow1" android:baselineAligned="true">
        <LinearLayout android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/linearLayout6">
            <ImageView android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageView1" android:src="@drawable/icon"></ImageView>
        </LinearLayout>
        <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1">
            <ImageView android:layout_width="wrap_content" android:id="@+id/imageView2" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_gravity="right"></ImageView>
        </LinearLayout>
    </TableRow>
    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:id="@+id/textView1" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
        <TextView android:id="@+id/textView4" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="top"></TextView>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
        <TextView android:id="@+id/textView5" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="top"></TextView>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
        <TextView android:id="@+id/textView6" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="top"></TextView>
    </LinearLayout>
    <Button android:layout_height="wrap_content" android:id="@+id/button2" android:layout_width="wrap_content" android:text="@string/f"></Button>
    <Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_width="wrap_content" android:text="@string/r"></Button>
    <LinearLayout android:layout_width="wrap_content" android:id="@+id/linearLayout5" android:layout_height="wrap_content" android:orientation="vertical">
        <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView10" android:text="TextView"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView9" android:text="TextView"></TextView>
        <TextView android:text="TextView" android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        <TextView android:text="TextView" android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    </LinearLayout>
</TableLayout>

Removed setContentView(R.layout.b); from onClick and it worked as expected, for me.

Try removing the call setContentView(xxxxxx) in the OnClick method. You already set the layout. By calling it again all the listeners for the buttons (maybe even the handles for the textViews, not sure) are removed.

Removed setContentView(R.layout.b); You are repeating the same thing!

public class ZARP extends Activity
{
       setContentView(R.layout.b);
      ...

         private void b(P p, E)
             {..........

add setContentView(R.layout.b); like the above and try.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM