Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Studio:使用intent传递多个值只提供1个值?_Java_Android Intent_Android Studio_Android Bundle - Fatal编程技术网

Java Android Studio:使用intent传递多个值只提供1个值?

Java Android Studio:使用intent传递多个值只提供1个值?,java,android-intent,android-studio,android-bundle,Java,Android Intent,Android Studio,Android Bundle,我试图做一个测验,用户在每个活动中选择一个答案,最后一页根据选择的选项提供答案。因此,我想将这些值传递给最终活动,但似乎只有最后选择的值显示出来 第一项活动: public class Quiz extends Activity { Button btn; RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我试图做一个测验,用户在每个活动中选择一个答案,最后一页根据选择的选项提供答案。因此,我想将这些值传递给最终活动,但似乎只有最后选择的值显示出来

第一项活动:

public class Quiz extends Activity {

Button btn;
RadioGroup rg;


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

    btn = (Button) findViewById(R.id.nextBtn);
    rg= (RadioGroup) findViewById(R.id.rg);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
            Intent intent = new Intent(getApplicationContext(), Quiz1.class);
            Bundle bundle = new Bundle();
            int id = rg.getCheckedRadioButtonId();
            RadioButton radioButton = (RadioButton) findViewById(id);
            bundle.putString("rg", radioButton.getText().toString());
            intent.putExtras(bundle);
            startActivity(intent);

        }

        }

    });
  }
}
public class Quiz1 extends Activity {

Button btn;
RadioGroup rg1;


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

    btn = (Button) findViewById(R.id.nextBtn1);
    rg1= (RadioGroup) findViewById(R.id.rg1);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg1.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent1 = new Intent(getApplicationContext(), Quiz2.class);
                Bundle bundle1 = new Bundle();
                int id = rg1.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle1.putString("rg1", radioButton.getText().toString());
                intent1.putExtras(bundle1);
                startActivity(intent1);

            }

        }

    });
  }
}
第二项活动:

public class Quiz extends Activity {

Button btn;
RadioGroup rg;


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

    btn = (Button) findViewById(R.id.nextBtn);
    rg= (RadioGroup) findViewById(R.id.rg);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
            Intent intent = new Intent(getApplicationContext(), Quiz1.class);
            Bundle bundle = new Bundle();
            int id = rg.getCheckedRadioButtonId();
            RadioButton radioButton = (RadioButton) findViewById(id);
            bundle.putString("rg", radioButton.getText().toString());
            intent.putExtras(bundle);
            startActivity(intent);

        }

        }

    });
  }
}
public class Quiz1 extends Activity {

Button btn;
RadioGroup rg1;


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

    btn = (Button) findViewById(R.id.nextBtn1);
    rg1= (RadioGroup) findViewById(R.id.rg1);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg1.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent1 = new Intent(getApplicationContext(), Quiz2.class);
                Bundle bundle1 = new Bundle();
                int id = rg1.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle1.putString("rg1", radioButton.getText().toString());
                intent1.putExtras(bundle1);
                startActivity(intent1);

            }

        }

    });
  }
}
下面是总共7项活动。。不包括最后的活动 以下是第7项(称为Quiz6)活动:

public class Quiz6 extends Activity {

Button btn;
RadioGroup rg6;

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


    btn = (Button) findViewById(R.id.nextBtn6);
    rg6= (RadioGroup) findViewById(R.id.rg6);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg6.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent6 = new Intent(getApplicationContext(), Final1.class);
                Bundle bundle6 = new Bundle();
                int id = rg6.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle6.putString("rg6", radioButton.getText().toString());
                intent6.putExtras(bundle6);
                startActivity(intent6);

            }

        }

    });
  }
}
你明白了:)

这是最后的活动(称为Final1),结果显示在这里

这是密码

public class Final1 extends Activity {

Button btn;

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

    Bundle bundle = getIntent().getExtras();

    TextView textView = (TextView)findViewById(R.id.txt);
    textView.setText(bundle.getCharSequence("rg"));

    Bundle bundle1 = getIntent().getExtras();
    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView.setText(bundle1.getCharSequence("rg1"));

    Bundle bundle2 = getIntent().getExtras();
    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView.setText(bundle2.getCharSequence("rg2"));

    Bundle bundle3 = getIntent().getExtras();
    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView.setText(bundle3.getCharSequence("rg3"));

    Bundle bundle4 = getIntent().getExtras();
    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView.setText(bundle4.getCharSequence("rg4"));

    Bundle bundle5 = getIntent().getExtras();
    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView.setText(bundle5.getCharSequence("rg5"));

    Bundle bundle6 = getIntent().getExtras();
    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView.setText(bundle6.getCharSequence("rg6"));

    btn = (Button)findViewById(R.id.restartBtn);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(v.getContext(), Quiz.class);
            startActivityForResult(in, 0);
        }
    });

  }

}
一旦我运行了程序,结果是只有“textView”最终会在最后一个活动之前更改为活动上的所选选项,如上图所示


感谢您的帮助,一项活动的意图只有一个与之相关的
包。因此,您只将最后一个bundle传递给最终活动,而将其他bundle传递给紧随其后的活动。您可以通过执行以下操作来解决此问题

Bundle bundle = getIntent().getExtras(); //this gets the current activity's extras
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
而不是像现在这样每次都创建一个新包。然后将此包传递给以下活动。这样,每次你得到一个新答案时,你都会把它添加到你的答案包中,而不是每次只创建一个新的答案包

然后在你的最后一个活动中,你只需要得到一个包,就可以从中得到所有的答案。注意:您需要确保您的电子邮件正在使用正确的文本更新正确的文本视图。在您的问题中,
textView
是唯一更新了6次的视图,而其他视图根本没有更新

Bundle bundle = getIntent().getExtras();

TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));

TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));

TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));

TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));

TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));

TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));

TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));

一个活动的意图只有一个与之关联的
包。因此,您只将最后一个bundle传递给最终活动,而将其他bundle传递给紧随其后的活动。您可以通过执行以下操作来解决此问题

Bundle bundle = getIntent().getExtras(); //this gets the current activity's extras
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
而不是像现在这样每次都创建一个新包。然后将此包传递给以下活动。这样,每次你得到一个新答案时,你都会把它添加到你的答案包中,而不是每次只创建一个新的答案包

然后在你的最后一个活动中,你只需要得到一个包,就可以从中得到所有的答案。注意:您需要确保您的电子邮件正在使用正确的文本更新正确的文本视图。在您的问题中,
textView
是唯一更新了6次的视图,而其他视图根本没有更新

Bundle bundle = getIntent().getExtras();

TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));

TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));

TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));

TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));

TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));

TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));

TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));

但是在我的第一个活动(测验)中,我不必首先做
Bundle bundle1=new Bundle()?。而且,我是否真的需要在每次活动后通过并接收它,或者我可以在最后一次活动中一次全部完成?因为我不知道你说的在下一个活动中收到它是什么意思,我不知道该怎么做:$对不起,我是个傻瓜。谢谢你说得对,在第一个活动中,你需要一个新的
捆绑包
。之后,您将获取传递到当前活动中的捆绑包,向其添加一个值,并将同一捆绑包传递到下一个活动。这样,当您结束时,bundle包含了您跨活动添加的所有内容。哦,我明白了:)但是我是否还必须添加一个代码,以便它在下一个活动中接收它,或者它是否会通过执行
bundle bundle=getIntent().getExtras()自动执行此操作,因为我仍然只得到末尾的最后一个值,这就是这种方法的优点。安卓为你做到了!它在活动接收到意图时保存它,您可以随时使用
getIntent()
访问它。因此,现在我有了
}else{intent intent intent=new intent(getApplicationContext(),Quiz6.class);//Bundle Bundle 5=new Bundle();Bundle=getIntent().getExtras();int id=rg5.getCheckedRadioButtonId();RadioButton RadioButton=(RadioButton)findViewById(id);bundle.putString(“rg5”,RadioButton.getText().toString());intent.putExtras(bundle);
在第一个活动中,我有
bundle bundle=new bundle();
而不是getIntent,仍然是相同的问题tho:(但在我的第一个活动(测验)中,我是否不必首先执行
Bundle bundle1=new Bundle()
?。还有,我是否真的必须在每次活动后传递和接收它,或者我可以在最后一次活动中一次性完成所有任务?因为我不知道你说的在下一次活动中接收它是什么意思,我不知道具体怎么做:$对不起,我是一个noob。谢谢你说得对,在第一次活动中,你会想要一个new
Bundle
。之后,您将获取传递到当前活动中的Bundle,为其添加值,并将相同的Bundle传递到下一个活动。这样,当您到达末尾时,该Bundle包含您跨活动添加的所有内容。哦,我明白了:)但是,我是否还必须添加一个代码,以便它在下一个活动中接收该代码,或者它是否会通过执行
Bundle Bundle=getIntent().getExtras()自动执行此操作;
,因为我仍然只得到末尾的最后一个值,这是这种方法的优点。Android为您做到了这一点!当活动接收到意图时,它会保存意图,您可以随时使用
getIntent()
访问它。所以现在我有了
}else{intent intent intent=new intent(getApplicationContext()),Quiz6.class);//Bundle Bundle 5=new Bundle();Bundle Bundle=getIntent().getExtras();int id=rg5.getCheckedRadioButtonId();RadioButton RadioButton=(RadioButton)findViewById(id);Bundle.putString(“rg5”,RadioButton.getText().toString());intent.putExtras(bundle);
和在第一个活动中