【问题标题】:How to pass string id's through intent?如何通过意图传递字符串ID?
【发布时间】:2015-09-02 16:16:08
【问题描述】:

我想通过意图发送整数值,并在另一个活动中访问它...我尝试使用以下代码..但它不起作用..!!

mCheatButton = (Button)findViewById(R.id.cheat_button);
    mCheatButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                mCheatButton.setVisibility(View.INVISIBLE);
                mIsCheater = false;
                int mCurrentIndex=2;
                Intent i = new Intent(quizactivity.this,       CheatActivity.class);
                i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, mCurrentIndex);
                startActivityForResult(i, 0);
            }
            catch (Exception e)
            {
            }

        }
    });


SECOND ACTIVITY:
          mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
    mShowAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(extras == null)

            {        mAnswerIsTrue= 0;
                updateanswer(mAnswerIsTrue);

            }

            else

            {        

                mAnswerIsTrue= extras.getInt(EXTRA_ANSWER_IS_TRUE);
                updateanswer(mAnswerIsTrue);
            }

            updateanswer(mAnswerIsTrue);
        }
    });

 public void updateanswer(int q){
    mAnswerTextView.setText(solution[q].getA());
    setAnswerShownResult(true);
}

请帮我解决..!!

我想通过意图发送整数值,并在另一个活动中访问它...我尝试使用以下代码..但它不起作用..!!

上面的代码有什么错误..我真的不明白...!!

【问题讨论】:

  • 您还没有告诉我们您在运行此代码时遇到的问题。此外,您的标题提到了字符串,但您的正文只谈论 int。此外,拥有空的 catch{}仍然是个坏主意
  • 更清楚问题 - 你得到它一次吗?绝不?它会崩溃吗?你从来没有任何额外的东西吗?

标签: java android android-intent


【解决方案1】:

我猜你的问题出在你的第二个活动上。

Intent extras = getIntent();
int yourInt = extras.getIntExtra(EXTRA_ANSWER_IS_TRUE, -1);

【讨论】:

    【解决方案2】:

    在你需要做的第二个活动中

    int i = getIntent().getExtras().getInt(CheatActivity.EXTRA_ANSWER_IS_TRUE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      相关资源
      最近更新 更多