0

我在 android 中做了一个简单的应用程序,有两个活动,在第一个活动中,它是一个包含一些编辑文本和单选组和一个复选框的表单。但是单选按钮有问题。它的值不会转到另一个活动。我试过了以下:

act1.java

   package com.example.myweb;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {
     public  String EXTRA_MESSAGE = "com.example.myweb";
     Button b;
     EditText ed1,ed2,ed3,ed4;
     RadioGroup rg;
     RadioButton rb;
     CheckBox c1;
    // boolean yes = c1.isChecked();


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

    rg=(RadioGroup)findViewById(R.id.radioGroup1);  
         b =(Button)findViewById(R.id.send);
         ed1=(EditText)findViewById(R.id.edit_msg);
         ed2=(EditText)findViewById(R.id.edit_msg1);
         ed3=(EditText)findViewById(R.id.edit_msg3);
         ed4=(EditText)findViewById(R.id.edit_msg4);
        // r1=(RadioButton)findViewById(R.id.radioButton1);
        // r2=(RadioButton)findViewById(R.id.radioButton2);
         c1=(CheckBox)findViewById(R.id.ck1);
         int id=rg.getCheckedRadioButtonId();
         rb=(RadioButton)findViewById(id);
                 b.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                         // TODO Auto-generated method stub


                        // Intent i = new Intent(MainActivity.this,Act2.class);
                     Bundle b=new Bundle();
                     final String fname=ed1.getText().toString();
                     final String lname=ed2.getText().toString();
                     final String fon=ed3.getText().toString();
                     final String city= ed4.getText().toString();
                     final String gen = rb.getText().toString();
                     if(c1.isChecked()){



                                Intent i = new Intent(MainActivity.this,Act2.class);

                                i.putExtra("Extra__fname", fname);
                                 i.putExtra("Extra__lname", lname);
                                 i.putExtra("Extra__fon", fon);
                                 i.putExtra("Extra__city", city);
                                 i.putExtra("gen", gen);
                                 startActivity(i);  
                    }
                    else{

                        Toast.makeText(getApplicationContext(), "Please accept it", 1).show();
                            }
                    }       
                     });
                    }
}

act2.java

  package com.example.myweb;

import org.w3c.dom.Text;

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

public class Act2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
        TextView t = (TextView)findViewById(R.id.tv1);
        TextView t1 = (TextView)findViewById(R.id.tv2);
        TextView t2 = (TextView)findViewById(R.id.tv3);
        TextView t3 = (TextView)findViewById(R.id.tv4);
        TextView t4 = (TextView)findViewById(R.id.tv5);
        Bundle bundle = getIntent().getExtras();

             String fname= bundle.getString("Extra__fname");
              String lname= bundle.getString("Extra__lname");
              String fon= bundle.getString("Extra__fon");
              String city= bundle.getString("Extra__city");
              String gen= bundle.getString("gen");
              t.setText(fname);
                t1.setText(lname);
                t2.setText(fon);
                t3.setText(city);
                t4.setText(gen);

                }
        }

日志猫

05-02 09:55:28.492: E/ActivityThread(554): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf6fe0 that was originally bound here
05-02 09:55:28.492: E/ActivityThread(554): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf6fe0 that was originally bound here
05-02 09:55:28.492: E/ActivityThread(554):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-02 09:55:28.492: E/ActivityThread(554):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-02 09:55:28.492: E/ActivityThread(554):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-02 09:55:28.492: E/ActivityThread(554):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-02 09:55:28.492: E/ActivityThread(554):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-02 09:55:28.492: E/ActivityThread(554):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-02 09:55:28.492: E/ActivityThread(554):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-02 09:55:28.492: E/ActivityThread(554):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
05-02 09:55:28.492: E/ActivityThread(554):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
05-02 09:55:28.492: E/ActivityThread(554):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
05-02 09:55:28.492: E/ActivityThread(554):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-02 09:55:28.492: E/ActivityThread(554):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-02 09:55:28.492: E/ActivityThread(554):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-02 09:55:28.492: E/ActivityThread(554):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-02 09:55:28.492: E/ActivityThread(554):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-02 09:55:28.492: E/ActivityThread(554):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-02 09:55:28.492: E/ActivityThread(554):  at java.lang.Thread.run(Thread.java:856)
05-02 09:55:28.592: E/StrictMode(554): null
05-02 09:55:28.592: E/StrictMode(554): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf6fe0 that was originally bound here
05-02 09:55:28.592: E/StrictMode(554):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-02 09:55:28.592: E/StrictMode(554):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-02 09:55:28.592: E/StrictMode(554):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-02 09:55:28.592: E/StrictMode(554):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-02 09:55:28.592: E/StrictMode(554):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-02 09:55:28.592: E/StrictMode(554):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-02 09:55:28.592: E/StrictMode(554):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-02 09:55:28.592: E/StrictMode(554):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
05-02 09:55:28.592: E/StrictMode(554):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
05-02 09:55:28.592: E/StrictMode(554):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
05-02 09:55:28.592: E/StrictMode(554):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-02 09:55:28.592: E/StrictMode(554):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-02 09:55:28.592: E/StrictMode(554):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-02 09:55:28.592: E/StrictMode(554):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-02 09:55:28.592: E/StrictMode(554):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-02 09:55:28.592: E/StrictMode(554):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-02 09:55:28.592: E/StrictMode(554):  at java.lang.Thread.run(Thread.java:856)
05-02 09:55:28.592: W/ActivityManager(274): Unbind failed: could not find connection for android.os.BinderProxy@410f7970
4

5 回答 5

1

采用

String gen= bundle.getString("gen");

代替

String gen= bundle.getString("Extra_gen");

RadioButton用于从先前活动发送的意图中检索选定的值。因为您正在发送带有键的 RadioButton 值,gen但尝试使用它来检索它Extra_gen

于 2013-05-02T04:23:34.373 回答
1

密钥应该相同

      i.putExtra("gen", gen);

它的

   String gen= bundle.getString("Extra_gen");

应该

   String gen= bundle.getString("gen");

编辑:

您正在使用 getApplicationContext()。而是使用活动上下文

    Toast.makeText(getApplicationContext(), "Please accept it", 1).show();

要知道何时使用应用程序上下文以及何时使用活动上下文,请在以下链接中查看 commonsware 的答案

何时调用活动上下文或应用程序上下文?

    Toast.makeText(act1.this, "Please accept it", 1).show();
于 2013-05-02T04:24:02.593 回答
0

采用

String gen= bundle.getString("gen");

你需要这样的东西

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
     void onCheckedChanged(RadioGroup rg, int checkedId) {
          for(int i=0; i<rg.getChildCount(); i++) {
               RadioButton btn = (RadioButton) rg.getChildAt(i);
               if(btn.getId() == checkedId) {
                    String text = btn.getText();
                    // do something with text
                    return;
               }
          }
     }
});

(或者)

当您需要获取选定的单选选项时。

int id = rg.getCheckedRadioButtonId();
if (id == -1){
     //no item selected
}
else{
if (id == R.id.radio_button1){
    //Do something with the button
}
}
于 2013-05-02T04:19:48.767 回答
0

用于setOnCheckedChangeListener无线电组。

示例代码RadioGroup包含两个RadioButtons

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(RadioGroup arg0, int arg1) {

            radioButton = (RadioButton) findViewById(radioGroup
                    .getCheckedRadioButtonId());
            if (radioButton.getText().equals("Name")) {
                // code for first radioButton
            } else {
                // code for second radioButton                  
            }
        }
    });

我希望这能帮到您。

于 2013-05-02T04:23:29.823 回答
0
int radioButtonID = rg.getCheckedRadioButtonId();
View radioButton = rg.findViewById(radioButtonID);
int index = rg.indexOfChild(radioButton);
String gen="male";

if(index==0)
gen="male";
else
gen="female";

....

i.putExtra("gen", gen);
startActivity(i);  

确保您的activity_main.xml 男性的索引为 0。

于 2013-05-02T04:27:01.973 回答