One Activity不会将Intent发送到另一个Activity

时间:2015-05-04 02:14:21

标签: java android android-intent

我遇到了问题:第一个活动(下面)没有向第二个活动发送意图。

这是MainActivity.java

package com.example.quizjavatest;


import android.os.Bundle;
import android.app.Activity;
import java.util.List;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity  {

    List<Question> quesList;
    int score=0;
    int qid=0;
    ImageView quizimg; 
    Question currentQ;
    TextView txtQuestion;
    RadioButton rda, rdb, rdc;
    Button butNext;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            DbHelper db=new DbHelper(this);
            quesList=db.getAllQuestions();
            currentQ=quesList.get(qid);

            txtQuestion=(TextView)findViewById(R.id.textView1); //question
            rda=(RadioButton)findViewById(R.id.radio0); //option A
            rdb=(RadioButton)findViewById(R.id.radio1); //option B
            rdc=(RadioButton)findViewById(R.id.radio2); //option C
            quizimg=(ImageView) findViewById(R.id.ImgQuiz); //img quiz
            butNext=(Button)findViewById(R.id.button1); //button next
            setQuestionView();


            butNext.setOnClickListener(new View.OnClickListener() {     
                @Override
                public void onClick(View v) {
                    RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
                    RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
                    Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
                    if(currentQ.getANSWER().equals(answer.getText()))
                    {
                        score++;
                        Log.d("score", "Your score"+score);
                    }
                    if(qid<5){                  
                        currentQ=quesList.get(qid);
                        setQuestionView();
                    }else{
                        Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
                        Bundle b = new Bundle();
                        b.putInt("score", score); //Your score
                        intent.putExtras(b); //Put your score to your next Intent
                        startActivity(intent);
                        finish();
                    }
                }
            });
}

        private void setQuestionView()
        {

            txtQuestion.setText(currentQ.getQUESTION());
            rda.setText(currentQ.getOPTA());
            rdb.setText(currentQ.getOPTB());
            rdc.setText(currentQ.getOPTC());
            quizimg.setImageBitmap(currentQ.getBitmap());
            qid++;
        }
}

ResultActivity.java

package com.example.quizjavatest;

import android.os.Bundle;
import android.app.Activity;

import android.widget.RatingBar;
import android.widget.TextView;

public class ResultActivity extends Activity {

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


        RatingBar bar=(RatingBar)findViewById(R.id.ratingBar1); 
        bar.setNumStars(5);
        bar.setStepSize(0.5f);
        //get text view
        TextView t=(TextView)findViewById(R.id.textResult);
        //get score
        Bundle b = getIntent().getExtras();
        int score= b.getInt("score");
        //display score
        bar.setRating(score);
        switch (score)
        {
        case 1:
        case 2: t.setText("Oopsie! Better Luck Next Time!");
        break;
        case 3:
        case 4:t.setText("Hmmmm.. Someone's been reading a lot of trivia");
        break;
        case 5:t.setText("Who are you? A trivia wizard???");
        break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

改变你的意图:

Intent intent = new Intent(this, ResultActivity.class); //better use `this` instead of global context
intent.putExtras("score", score); //Put your score to your next 
startActivity(intent);
finish();

除非您传递BundleObject的额外内容Intent,否则无需使用Bundle,不要两次执行此操作。

ResultActivity抓住它:

Intent intent = getIntent();
int score = intent.getIntExtra("score");

希望这能解决您的问题。