textview.setText不能在我的代码中工作

时间:2017-05-29 14:16:46

标签: java android

以下所有setText方法均无效。我不明白为什么。 有人可以帮忙吗?

//imports

 public class Main2Activity2 extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;
TextView P1Name;
TextView P2Name;
TextView P3Name;
TextView P4Name;



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    setContentView(R.layout.activity_main2);
    rb1 = (RadioButton) findViewById(R.id.radioButton1);
    rb1.setOnCheckedChangeListener(this);
    rb2 = (RadioButton) findViewById(R.id.radioButton2);
    rb2.setOnCheckedChangeListener(this);
    rb3 = (RadioButton) findViewById(R.id.radioButton3);
    rb3.setOnCheckedChangeListener(this);
    rb4 = (RadioButton) findViewById(R.id.radioButton4);
    rb4.setOnCheckedChangeListener(this);
    return true;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        if (buttonView.getId() == R.id.radioButton1) {
            rb2.setChecked(false);
            rb3.setChecked(false);
            rb4.setChecked(false);
        }
        if (buttonView.getId() == R.id.radioButton2) {
            rb1.setChecked(false);
            rb3.setChecked(false);
            rb4.setChecked(false);
        }
        if (buttonView.getId() == R.id.radioButton3) {
            rb1.setChecked(false);
            rb2.setChecked(false);
            rb4.setChecked(false);
        }
        if (buttonView.getId() == R.id.radioButton4) {
            rb1.setChecked(false);
            rb2.setChecked(false);
            rb3.setChecked(false);
        }
    }
}

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

        P1Name = (TextView) findViewById(R.id.PlayerName1);
        P2Name = (TextView) findViewById(R.id.PlayerName2);
        P3Name = (TextView) findViewById(R.id.PlayerName3);
        P4Name = (TextView) findViewById(R.id.PlayerName4);

        Intent intent = getIntent();
        String P1 = intent.getStringExtra("Player1Name");
        String P2 = intent.getStringExtra("Player2Name");
        String P3 = intent.getStringExtra("Player3Name");
        String P4 = intent.getStringExtra("Player4Name");

        P1Name.setText("hello");
        Log.d("string content: ", P1Name.getText() + " <---- ");
        P2Name.setText("working");
        Log.d("string content: ", P2Name.getText() + " <---- ");
        P3Name.setText(P3.toString());
        Log.d("string content: ", P3Name.getText() + " <---- ");
        P4Name.setText(P4);


    }
}

这是xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="katerji.malek.score.Main2Activity2">

<EditText
    android:layout_width="85sp"
    android:layout_height="wrap_content"
    android:inputType="numberSigned"
    android:ems="10"
    android:id="@+id/editText3"
    android:layout_below="@+id/tableLayout"
    android:layout_alignLeft="@+id/tableLayout"
    android:layout_alignStart="@+id/tableLayout"
    android:textAlignment="center"
    android:hint="score" />

<EditText
    android:layout_width="85sp"
    android:layout_height="wrap_content"
    android:inputType="numberSigned"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_below="@+id/tableLayout"
    android:layout_alignBaseline="@+id/editText3"
    android:layout_alignBottom="@+id/editText3"
    android:layout_toRightOf="@+id/editText3"
    android:layout_toEndOf="@+id/editText3"
    android:textAlignment="center"
    android:hint="score" />

<EditText
    android:layout_width="85sp"
    android:layout_height="wrap_content"
    android:inputType="numberSigned"
    android:ems="10"
    android:id="@+id/editText4"
    android:layout_below="@+id/tableLayout"
    android:layout_alignBaseline="@+id/editText"
    android:layout_alignBottom="@+id/editText"
    android:layout_toRightOf="@+id/editText"
    android:layout_toEndOf="@+id/editText"
    android:textAlignment="center"
    android:hint="score" />

<EditText
    android:layout_width="85sp"
    android:layout_height="wrap_content"
    android:inputType="numberSigned"
    android:ems="10"
    android:id="@+id/editText5"
    android:layout_below="@+id/tableLayout"
    android:textAlignment="center"
    android:hint="score"
    android:layout_alignBaseline="@+id/editText4"
    android:layout_alignBottom="@+id/editText4"
    android:layout_toRightOf="@+id/editText4"
    android:layout_toEndOf="@+id/editText4" />

<RadioButton
    android:layout_width="40dp"
    android:layout_height="40sp"
    android:id="@+id/radioButton1"
    android:layout_below="@+id/editText"
    android:layout_alignLeft="@+id/editText3"
    android:layout_alignStart="@+id/editText3"
    android:checked="false" />

<Button
    android:layout_height="40sp"
    android:id="@+id/button"
    android:layout_below="@+id/editText3"
    android:layout_toRightOf="@+id/radioButton1"
    android:text="\\/"
    android:layout_width="45sp" />

<RadioButton
    android:layout_width="40dp"
    android:layout_height="40sp"
    android:id="@+id/radioButton2"
    android:textAlignment="center"
    android:layout_below="@+id/editText"
    android:layout_toRightOf="@+id/button"
    android:layout_toEndOf="@+id/button" />

<Button
    android:layout_height="40sp"
    android:id="@+id/button4"
    android:text="\\/"
    android:layout_width="45sp"
    android:layout_below="@+id/editText"
    android:layout_toRightOf="@+id/radioButton2"
    android:layout_toEndOf="@+id/radioButton2" />

<RadioButton
    android:layout_width="40dp"
    android:layout_height="40sp"
    android:id="@+id/radioButton3"
    android:textAlignment="center"
    android:layout_below="@+id/editText4"
    android:layout_toRightOf="@+id/editText"
    android:layout_toEndOf="@+id/editText" />

<Button
    android:layout_height="40sp"
    android:id="@+id/button6"
    android:text="\\/"
    android:layout_width="45sp"
    android:layout_below="@+id/editText4"
    android:layout_toRightOf="@+id/radioButton3"
    android:layout_toEndOf="@+id/radioButton3" />

<RadioButton
    android:layout_width="30dp"
    android:layout_height="40sp"
    android:id="@+id/radioButton4"
    android:textAlignment="center"
    android:layout_below="@+id/editText5"
    android:layout_alignLeft="@+id/editText5"
    android:layout_alignStart="@+id/editText5" />

<Button
    android:layout_height="40sp"
    android:id="@+id/button7"
    android:text="\\/"
    android:layout_width="45sp"
    android:layout_below="@+id/editText5"
    android:layout_toRightOf="@+id/radioButton4"
    android:layout_toEndOf="@+id/radioButton4" />

<Button
    android:text="Sajjil"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:id="@+id/button8"
    android:layout_width="200sp"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true" />

<Button
    android:text="New Game"
    android:layout_width="200sp"
    android:layout_height="wrap_content"
    android:layout_marginTop="12dp"
    android:id="@+id/button10"
    android:layout_below="@+id/button8"
    android:layout_alignLeft="@+id/button8"
    android:layout_alignStart="@+id/button8" />

<TableLayout
    android:layout_height="wrap_content"
    android:layout_width="340dp"
    android:id="@+id/tableLayout"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:text="0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView13"
            android:textAlignment="center" />

        <TextView
            android:text="0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView12"
            android:textAlignment="center" />

        <TextView
            android:text="0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView11"
            android:textAlignment="center" />

        <TextView
            android:text="0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView10"
            android:textAlignment="center" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:text="Player 1"
            android:layout_height="wrap_content"
            android:id="@+id/PlayerName1"
            android:layout_width="85sp"
            android:textAlignment="center" />

        <TextView
            android:text="Player2"
            android:layout_height="wrap_content"
            android:id="@+id/PlayerName2"
            android:layout_width="85sp"
            android:textAlignment="center" />

        <TextView
            android:text="Player3"
            android:layout_height="wrap_content"
            android:id="@+id/PlayerName3"
            android:layout_width="85sp"
            android:textAlignment="center" />

        <TextView
            android:text="Player4"
            android:layout_height="wrap_content"
            android:id="@+id/PlayerName4"
            android:layout_width="85sp"
            android:textAlignment="center" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableRow>

</TableLayout>

2 个答案:

答案 0 :(得分:0)

也许尝试在末尾用toString声明你的String变量,如下所示:

    P3Name.setText(P3.toString());

答案 1 :(得分:0)

我删除了implements声明CompoundButton.OnCheckedChangeListener 以及两种方法:nCreateOptionsMenuonCheckedChanged。 这是工作代码:

public class Main2Activity2 extends AppCompatActivity  {

RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;
TextView P1Name;
TextView P2Name;
TextView P3Name;
TextView P4Name;



    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

        P1Name = (TextView) findViewById(R.id.PlayerName1);
        P2Name = (TextView) findViewById(R.id.PlayerName2);
        P3Name = (TextView) findViewById(R.id.PlayerName3);
        P4Name = (TextView) findViewById(R.id.PlayerName4);

        Intent intent = getIntent();
        String P1 = intent.getStringExtra("Player1Name");
        String P2 = intent.getStringExtra("Player2Name");
        String P3 = intent.getStringExtra("Player3Name");
        String P4 = intent.getStringExtra("Player4Name");
        P1Name.setText(P1);
        P2Name.setText(P2);
        P3Name.setText(P3);
        P4Name.setText(P4);


    }

}