0

I have made a simple application in android with two activities,in 1st activity ,it's a form containing some editexts and radio group and a checkbox.but i have problem with radio button.its value is not going to another activity.i have tried as below:

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);

                }
        }

Logcat

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
jigar
  • 1,423
  • 5
  • 23
  • 46
  • http://stackoverflow.com/questions/14111677/com-android-exchange-exchangeservice-has-leaked-error-when-running-emulator. You are not getting null pointer exception now. create a new avd delete the old one. Try running with a new avd or a device – Raghunandan May 02 '13 at 10:56

5 Answers5

1

use

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

instead of

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

for retrieving selected RadioButton Value from Intent sended from previous Activity. because you are sending RadioButton Value with gen key but trying to retrieve it using Extra_gen

ρяσѕρєя K
  • 127,886
  • 50
  • 184
  • 206
1

The key should be the same

      i.putExtra("gen", gen);

Its

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

should be

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

Edit:

You are using getApplicationContext(). Instead use a activity context

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

To know when to use application context and when to use activity context, check the answer by commonsware in the below link

When to call activity context OR application context?

    Toast.makeText(act1.this, "Please accept it", 1).show();
Community
  • 1
  • 1
Raghunandan
  • 129,147
  • 24
  • 216
  • 249
0

Use

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

You need something like this

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;
               }
          }
     }
});

(OR)

When you need to get the selected radio option.

int id = rg.getCheckedRadioButtonId();
if (id == -1){
     //no item selected
}
else{
if (id == R.id.radio_button1){
    //Do something with the button
}
}
Linga
  • 9,691
  • 9
  • 45
  • 91
0

use setOnCheckedChangeListener for radioGroup.

Sample code for RadioGroup which contains two 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                  
            }
        }
    });

I hope this will help you.

Gunaseelan
  • 11,546
  • 9
  • 64
  • 109
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);  

Make sure that in your activity_main.xml male is having index 0.

Arun C
  • 8,907
  • 1
  • 25
  • 42