【问题标题】:Passing a counter through an intent to a new activity通过意图将计数器传递给新活动
【发布时间】:2017-03-22 08:27:56
【问题描述】:

我正在开发一个儿童学习发展应用程序,我想在屏幕顶部显示一个分数,以显示他们到目前为止答对了多少问题。我有一个 if 语句,说明他们是否用正确的字母拼写答案,然后他们将进入下一个活动,但随着他们的移动,我希望他们的分数更新。希望通过意图将计数器传递给第二个活动,但是当我单击提交按钮时,应用程序崩溃了。 这是第一堂课

package com.helloworld.erica.alphaanimals;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import static com.helloworld.erica.alphaanimals.R.id.imageView;
public class button_a extends Activity {

private Button button_b;
TextView scoreText;
int counter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.button_a);

    //final TextView score = (TextView) findViewById(R.id.score);

    final ImageView ImageView = (ImageView) findViewById(imageView);
    Button submit = (Button) findViewById(R.id.submit);

    submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            EditText answerA = (EditText) findViewById(R.id.answerA);
            String toCompare = answerA.getText().toString();
            TextView score = (TextView)findViewById(R.id.score);


            if(toCompare.startsWith("A")) {
                Intent intent_b = new Intent(button_a.this, button_b.class);
                intent_b.putExtra("username", score.getText().toString());
                startActivity(intent_b);
                counter++;
                scoreText.setText(Integer.toString(counter));

                Toast.makeText(button_a.this, "Well Done, correct answer!!", Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(button_a.this, "Incorrect", Toast.LENGTH_SHORT).show();

            }
        }
    });

}

} 这是第二个

package com.helloworld.erica.alphaanimals;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; 
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class button_b extends Activity {
private Button button_c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.button_b);
    final ImageView ImageView = (ImageView) findViewById(imageView);
    Button submit = (Button) findViewById(R.id.submit);
    TextView scoreText = (TextView) findViewById(R.id.scoreText);
    scoreText.setText(getIntent().getExtras().getString("username"));

    submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            EditText answerB = (EditText) findViewById(R.id.answerB);
            String toCompare = answerB.getText().toString();

            if(toCompare.startsWith("B")) {
                Intent intent_c = new Intent(button_b.this, button_c.class);
                startActivity(intent_c);
                //ImageView.setImageResource(R.drawable.bear);
                Toast.makeText(button_b.this, "Well Done, correct answer!!", Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(button_b.this, "Incorrect", Toast.LENGTH_SHORT).show();

            }
        }
    });

}

}

【问题讨论】:

  • 请发布崩溃的堆栈跟踪。
  • 设置 scoretext "counter++; scoreText.setText(Integer.toString(counter));"意图之前
  • 您的scoreText 在您的button_a活动中是null。在开始新活动之前,也可以调用此counter++; scoreText.setText(Integer.toString(counter));
  • 让它停止崩溃,不得不改变 scoreText.setText(Integer.toString(counter)); to score.setText(Integer.toString(counter));
  • 在下面查看我的答案

标签: android android-intent counter


【解决方案1】:

您在设置之前获取文本,因此进行了以下更改:

            counter++;
            scoreText.setText(Integer.toString(counter));
intent_b.putExtra("username", score.getText().toString());
            startActivity(intent_b);

【讨论】:

    【解决方案2】:

    您应该在 Android Studio 中检查 Android Monitor 上的错误,将显示错误内容,您可以将其粘贴到此处。这将有助于解决您的问题。

    【讨论】:

      【解决方案3】:
          if(toCompare.startsWith("A")) {
      
                          counter++;
                          score.setText(Integer.toString(counter));
      
                          Toast.makeText(button_a.this, "Well Done, correct answer!!", Toast.LENGTH_SHORT).show();
      
                          Intent intent_b = new Intent(button_a.this, button_b.class);
                          intent_b.putExtra("username", score.getText().toString());
                          startActivity(intent_b);
      
                      }else{
                          Toast.makeText(button_a.this, "Incorrect", Toast.LENGTH_SHORT).show();
      
                      }
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        第一个活动中没有 scoreText。您的 TextView 名称是分数,因此请更改它,并且语句的顺序也不正确。 Intent 应该在 counter++ 之后并在 TextView 中设置文本。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-07
          • 1970-01-01
          • 1970-01-01
          • 2018-01-18
          • 2020-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多