无法将int的值放入另一个活动中

时间:2018-06-17 10:58:48

标签: android android-activity

嗨我需要将第一个活动的值放到第二个。在“原始”我希望通过单击按钮增加值,但现在,我只是创建了一个存储值2的测试变量。现在,我想将此值发送到另一个活动,但我做错了,因为在第二活动,我的文本视图总是显示0.我怎么能解决这个问题?

头等舱

爪哇

public class MainActivity extends AppCompatActivity {

int valueTV;
int test = 2;
Button button;

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

    Intent intent = new Intent(this,showScore.class);
    intent.putExtra("",test);
    startActivity(intent);

    button = (Button) findViewById(R.id.button2);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            goToNextActivity();
        }
    });
}

public void incrementTV(View view) {
    valueTV = valueTV + 1;
    displayValue(valueTV);
}

private void displayValue(int number) {
    TextView textView = (TextView) findViewById(R.id.ID);
    textView.setText(String.valueOf(number));
}

public void goToNextActivity() {
    Intent intent = new Intent(this,showScore.class);
    startActivity(intent);
}
}

XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test"
    android:textSize="100sp"
    />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/add_btn"
    android:onClick="incrementTV"
    android:layout_below="@+id/ID"/>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/button"
    android:text="Button"
    android:onClick="goToNextActivity"/>


</RelativeLayout>

第二课

Java

public class showScore extends AppCompatActivity {

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

    Intent intent = getIntent();
    int value = intent.getIntExtra("",0);
}

public void displayValue(int number){
    TextView textView = (TextView) findViewById(R.id.random_id);
    textView.setText(String.valueOf(number));
}

}

XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".showScore">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/random_id"
    android:text="@string/test_2"
    android:textSize="100sp"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

检查以下代码

   public class MainActivity extends AppCompatActivity {

int valueTV;
int test = 2;
Button button;

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



    button = (Button) findViewById(R.id.button2);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            goToNextActivity();
        }
    });
}

public void incrementTV(View view) {
    valueTV = valueTV + 1;
    displayValue(valueTV);
}

private void displayValue(int number) {
    TextView textView = (TextView) findViewById(R.id.ID);
    textView.setText(String.valueOf(number));
}

public void goToNextActivity() {
    Intent intent = new Intent(MainActivity.this,showScore.class);
    intent.putExtra("",test);
    startActivity(intent);
}
}
showmore class

中的

    public class showScore extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_score);
    int number = 0;
    Intent intent = getIntent();
    int value = intent.getIntExtra("",number);
    displayValue(number)
}

public void displayValue(int number){
    TextView textView = (TextView) findViewById(R.id.random_id);
    textView.setText(String.valueOf(number));
}

}

答案 1 :(得分:0)

所以我现在有另一个问题。就像我说的,我想通过活动传递数据,所以我已经做出了3个独立的integeres应该发送到第二个活动,但我不知道为什么,模拟器不断添加所有这些。基本上,我做一个应用程序,在我的课程的情况下 - 我需要创建一个应用程序,保持得分,在我的情况下,应用程序将保持足球比赛期间的得分,犯规次数和核心人数。要设置数据,我们点击特殊按钮(按分数增加分数等),当我们想要进入第二个活动并看到我们应该看到的详细统计数据,如3个目标,6个犯规,以及团队的2个角落在比赛期间“A”,但是当我点击按钮时(假设是1次进球,2次犯规,3次角球,应用应该像1个球,2个犯规,3个角但是它会继续添加所有对于整体而言,它就像是6个进球,6个犯规,6个角球。可能出现什么问题?

计算活动(这里程序存储整数的值)

public class CountActivity extends AppCompatActivity {
public int team_a_score;
public int team_a_foul;
public int team_a_corner;

public int team_b_score;
public int team_b_foul;
public int team_b_corner;

private Button goToDetailedStats;


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

    goToDetailedStats = (Button) findViewById(R.id.go_to_stats);
    goToDetailedStats.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openDetailedScore();
        }
    });

}

private void openDetailedScore() {
    Intent intent = new Intent(this,DeatiledScoreActivity.class);
    intent.putExtra("",team_a_score);
    intent.putExtra("",team_a_foul);
    intent.putExtra("",team_a_corner);
    startActivity(intent);
}

public void resetScore(View view) {
    team_a_score = 0;
    display_a_score(team_a_score);

    team_b_score = 0;
    display_b_score(team_b_score);
}

private void display_a_score (int a_score){
    TextView team_a_score_tv = (TextView) findViewById(R.id.team_a_score_tv);
    team_a_score_tv.setText(String.valueOf(a_score));
}
private void display_b_score (int b_score){
    TextView team_b_score_tv = (TextView) findViewById(R.id.team_b_score_tv);
    team_b_score_tv.setText(String.valueOf(b_score));
}

public void increment_a_score(View view) {
    team_a_score = team_a_score+1;
    display_a_score(team_a_score);
}

public void increment_a_foul(View view) {
    team_a_foul = team_a_foul+1;
}

public void increment_a_corner(View view) {
    team_a_corner = team_a_corner+1;
}

public void increment_b_score(View view) {
    team_b_score = team_b_score+1;
    display_b_score(team_b_score);
}

public void increment_b_foul(View view) {
    team_b_foul = team_b_foul+1;
}

public void increment_b_corner(View view) {
    team_b_corner = team_b_corner+1;
}
} 

详细活动必须接收我们作为意图从计数活动发送的数据。

public class DeatiledScoreActivity extends AppCompatActivity {

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

    int team_a_score_detail_int = 0;
    int team_a_foul_detail_int = 0;
    int team_a_corner_detail_int = 0;

    int team_b_score_detail_int = 0;
    int team_b_foul_detail_int = 0;
    int team_b_corner_detail_int = 0;

    Intent goal_intent = getIntent();
    int goal = goal_intent.getIntExtra("", team_a_score_detail_int);
    display_a_goal(goal);

    Intent foul_intent = getIntent();
    int foul = foul_intent.getIntExtra("", team_a_foul_detail_int);
    display_a_foul(foul);

    Intent corner_intent = getIntent();
    int corner = corner_intent.getIntExtra("", team_a_corner_detail_int);
    display_a_corner(corner);

}

public void display_a_goal(int score) {
    TextView a_score = (TextView) findViewById(R.id.team_a_goal_detail);
    a_score.setText(String.valueOf(score));
}

public void display_a_foul(int foul){
TextView a_foul = (TextView) findViewById(R.id.team_a_foul_detail);
    a_foul.setText(String.valueOf(foul));
}

public void display_a_corner(int corner){
    TextView a_corner = (TextView) findViewById(R.id.team_a_corner_detail);
    a_corner.setText(String.valueOf(corner));
}
}