TextView上的setText(),不在我的代码中工作

时间:2017-10-30 14:32:28

标签: android if-statement settext

我正在制作一款应用。当我尝试在TextView上设置文本时,它不起作用!

scoreteam_a和scoreteam_b没有setText()。 if语句也不起作用。请帮我。我的代码有什么问题? teama和teamb setText()方法有效但不在scoreteam_a和scoreteam_b上。 我直接从方法中的活动访问变量。

这是我的代码:

package com.example.android.courtcounter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

public class FinishActivity extends AppCompatActivity {
Bundle bundle;
String teama_name;
String teamb_name;
int scoreteama;
int scoreteamb;
String scoreTeamAString;
String scoreTeamBString;
private TextView winnera;
private TextView winnerb;
private TextView draw;
private TextView teama;
private TextView teamb;
private TextView scoreteam_a;
private TextView scoreteam_b;

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



    bundle = getIntent().getExtras();

    winnera = (TextView) findViewById(R.id.winnera);
    winnerb = (TextView) findViewById(R.id.winnerb);
    draw = (TextView) findViewById(R.id.draw);
    teama = (TextView) findViewById(R.id.teama);
    teamb = (TextView) findViewById(R.id.teamb);
    scoreteam_a = (TextView) findViewById(R.id.scoreteama);
    scoreteam_b = (TextView) findViewById(R.id.scoreteamb);

    teama_name = MainActivity.teamAName;
    teamb_name = MainActivity.teamBName;
    scoreteama = ScoringActivity.scoreTeamA;
    scoreteamb = ScoringActivity.scoreTeamB;
    scoreTeamAString = ScoringActivity.stringTeamA;
    scoreTeamBString = ScoringActivity.stringTeamB;

    Animation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(50); //You can manage the blinking time with this parameter
    anim.setStartOffset(20);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);

    if (scoreteama > scoreteamb) {
        winnera.setVisibility(View.VISIBLE);
        winnera.startAnimation(anim);
    } else if (scoreteamb > scoreteama) {
        winnerb.setVisibility(View.VISIBLE);
        winnerb.startAnimation(anim);
    } else if (scoreteamb == scoreteama) {
        draw.setVisibility(View.VISIBLE);
        draw.startAnimation(anim);
    }

    teama.setText(teama_name);
    teamb.setText(teamb_name);
    scoreteam_a.setText(ScoringActivity.stringTeamA);
    scoreteam_b.setText(ScoringActivity.stringTeamB);



    TextView shareButton = (TextView) findViewById(R.id.share);
    shareButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, teama_name + " vs " + teamb_name + "\n" + teama_name + " = " + scoreteama + "\n" + teamb_name + " = " + scoreteamb);
            startActivity(shareIntent);
        }
    });
}

public void newmatch(View v) {
    Intent intent = new Intent(FinishActivity.this, MainActivity.class);
    startActivity(intent);
    winnera.setVisibility(View.INVISIBLE);
    winnerb.setVisibility(View.INVISIBLE);
    draw.setVisibility(View.INVISIBLE);
    winnera.clearAnimation();
    winnerb.clearAnimation();
    draw.clearAnimation();
   }
   }

1 个答案:

答案 0 :(得分:-1)

您无法将整数设置为Texview。

尝试使用textview.setText(String.valueOf(scoreteama))