无法在textview

时间:2016-05-16 07:52:15

标签: java android textview sum

我是初学者,我正在尝试制作没有数据库和多项活动的测验游戏。我希望将评分系统作为所有活动中的文本视图,并且应该根据正确或错误的答案进行更改。我一直试图弄清楚它已经超过一个星期了,我只觉得我浪费了那个时间。这是代码。问题在于添加代码...与int和string有关...我对这些术语不太好所以如果有人可以输入代码是什么,如果我需要改变任何东西它会拯救我从这场噩梦......:)

page5.java

    public class Page5 extends AppCompatActivity {

    int score;

TextView scored;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);;
    setContentView(R.layout.activity_page5);
    scored = (TextView) findViewById(R.id.score1);

    Intent mintent = getIntent();
   int score = mintent.getIntExtra("score",0);
    scored.setText(String.valueOf(score));

}
public void rightanswer(View view){

    score = score + Integer.parseInt(scored.getText());
    scored.setText(String.valueOf(score));
    Intent intent = new Intent(this, Page6.class);
    intent.putExtra("ta_score",score);
    startActivity(intent);
    ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,
            0, view.getWidth(), view.getHeight());
    startActivity(intent, options.toBundle());

}


 }

activity_page5

      <LinearLayout
    android:layout_width="50dp"
    android:layout_height="80dp"
    android:layout_weight="1"
    android:background="#05083e"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Score"
        android:id="@+id/tvLabelScore"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#f6f6f6"
        android:layout_marginTop="5dp"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/score1"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#e5e5e5"
        android:layout_gravity="center_horizontal" />
  </LinearLayout>

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"

    android:onClick="rightanswer"
    />



    </RelativeLayout>

Page4.java

      public class Page4 extends AppCompatActivity {
   int score = 0;
    TextView scored1;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);;
    setContentView(R.layout.activity_page4);
    scored1 = (TextView) findViewById(R.id.score);
    scored1.setText(String.valueOf(score));
}
public void nocircle(View view){
    score = score + 5;
    scored1.setText(String.valueOf(score));
    Intent intent = new Intent(this,Page5.class);
    intent.putExtra("score",score);
    startActivity(intent);
    ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,
            0, view.getWidth(), view.getHeight());
    startActivity(intent, options.toBundle());
   }
    }      

activity_page4

     <LinearLayout
        android:layout_width="50dp"
        android:layout_height="80dp"
        android:layout_weight="1"
        android:background="#05083e"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Score"
            android:id="@+id/tvLabelScore"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textColor="#f6f6f6"
            android:layout_marginTop="5dp"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/score"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textColor="#e5e5e5"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
     <TextView
    android:layout_width="180dp"
    android:layout_height="100dp"
    android:layout_below="@+id/imageView10"
    android:text="NO"
    android:gravity="center"
    android:fontFamily="casual"
    android:textSize="70sp"
    android:textColor="#fff"
    android:layout_toRightOf="@+id/answeryes"
    android:onClick="nocircle"
    />

1 个答案:

答案 0 :(得分:0)

您正在接收局部变量的值。改变

int score = mintent.getIntExtra("score",0); 

score = mintent.getIntExtra("score",0);

完整代码:

public class Page5 extends AppCompatActivity {

int score;

TextView scored;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);;
setContentView(R.layout.activity_page5);
scored = (TextView) findViewById(R.id.score1);

Intent mintent = getIntent();
score = mintent.getIntExtra("score",0);
scored.setText(String.valueOf(score));

}
public void rightanswer(View view){

score = score + Integer.parseInt(scored.getText());
scored.setText(String.valueOf(score));
Intent intent = new Intent(this, Page6.class);
intent.putExtra("ta_score",score);
startActivity(intent);
ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,
        0, view.getWidth(), view.getHeight());
startActivity(intent, options.toBundle());
}
}