Android TextView上的setText(),在我的代码中不起作用
我正在制作一个应用程序。当我试图在文本视图上设置文本时,它不起作用 scoreteam_a和scoreteam_b不设置xt()。 if语句也不起作用。请帮帮我。我的代码出了什么问题? teama和teamb setText()方法对scoreteam_a和scoreteam_b有效,但对scoreteam_a和scoreteam_b无效。 我直接从方法中的活动访问变量 这是我的密码:Android TextView上的setText(),在我的代码中不起作用,android,if-statement,settext,Android,If Statement,Settext,我正在制作一个应用程序。当我试图在文本视图上设置文本时,它不起作用 scoreteam_a和scoreteam_b不设置xt()。 if语句也不起作用。请帮帮我。我的代码出了什么问题? teama和teamb setText()方法对scoreteam_a和scoreteam_b有效,但对scoreteam_a和scoreteam_b无效。 我直接从方法中的活动访问变量 这是我的密码: package com.example.android.courtcounter; import andro
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();
}
}
不能将integer设置为Texview
尝试使用
textview.setText(String.valueOf(scoreteama))
他试图将文本设置为其他活动textview,这是完全没有意义的。我不知道他在其他活动中做什么,他在代码中也没有给出任何信息。所以问题很可能就是因为这个原因。Viktor我并没有试图将文本设置为其他活动textView,而是从另一个活动访问变量。但是if语句呢?Can;我看不到问题中的代码,他正在设置setText(scoreteama)
,我只看到scoreteam\u a.setText(ScoringActivity.stringTeamA)代码>和scoreteam_b.setText(ScoringActivity.stringTeamB)代码>你怎么知道这些变量是int
s,而不是它们的名字所暗示的String
s?你永远不应该这样做。要将数据从“活动”传递到另一个“活动”,应使用Intents或STARTFRESULT并设置结果。有很多不同的方法可以实现这一点(RxJava、EventBus、ContentProvider…),为什么不使用这个捆绑包?为什么要创造呢?为什么将其他活动的值放入此活动的字符串变量中,并且仍然使用其他活动的值来设置文本而不是新创建的局部变量?为什么在ScoringActivity中有这么多公共静态声明变量,而您可以使用该捆绑包呢;在将变量设置为文本视图之前,是否打印ScoringActivity.scoreTeamA
、ScoringActivity.scoreTeamB
和ScoringActivity.stringTeamB
变量?只需使用Log.d(“TAG”,“ScoringActivity.stringTeamA:”+ScoringActivity.stringTeamA)代码>。我猜你不会得到预期的结果。通过静态引用从其他活动获取结果的做法是错误的。