2013-05-02 26 views
0

我在android中做了一個簡單的應用程序,其中有兩個活動,第一個活動,它是一個包含一些editexts和radio組以及checkbox的表單。但是我有單選按鈕的問題。另一個activity.i已經試過如下:radiogroup在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); 

       } 
     } 

logcat的

05-02 09:55:28.492: E/ActivityThread(554): Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]f6fe0 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 [email protected]f6fe0 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 [email protected]f6fe0 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 [email protected] 
+0

http:// stackoverflow.com/questions/14111677/com-android-exchange-exchangeservice-has-leaked-error-when-running-emulator。您現在沒有收到空指針異常。創建一個新的AVD刪除舊的。嘗試運行一個新的AVD或設備 – Raghunandan 2013-05-02 10:56:00

回答

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在下面的鏈接

When to call activity context OR application context?

Toast.makeText(act1.this, "Please accept it", 1).show(); 
+0

仍然有問題...同樣的問題.. :( – jigar 2013-05-02 05:11:46

+0

@ user2334694你會得到任何異常或錯誤? – Raghunandan 2013-05-02 05:49:50

+0

當我運行projest運行良好,但是當我點擊第一個屏幕的按鈕它顯示「不幸您的應用程序已停止」。 – jigar 2013-05-02 14:49:55

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

(OR)

當你需要獲得所選擇的無線選項。

int id = rg.getCheckedRadioButtonId(); 
if (id == -1){ 
    //no item selected 
} 
else{ 
if (id == R.id.radio_button1){ 
    //Do something with the button 
} 
} 
+0

我不明白...你說什麼。 – jigar 2013-05-02 04:21:36

+0

不回答的問題可以發表相同的評論 – Raghunandan 2013-05-02 04:21:57

+0

請看我編輯了代碼,因爲你說..但仍然有問題 – jigar 2013-05-02 06:34:26

0

對radioGroup使用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     
      } 
     } 
    }); 

示例代碼我希望這會幫助你。的

+0

請看我編輯了代碼,因爲你說..但仍有問題 – jigar 2013-05-02 06:34:51

+0

什麼你現在面臨的問題是什麼? – Gunaseelan 2013-05-02 06:57:20

+0

當我運行我的app.the顯示的第一個活動,並填寫完所有細節後,當我按下按鈕..它顯示「不幸的是你的應用程序停止」在模擬器中。 – jigar 2013-05-02 09:40:01

1

使用

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

代替

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

檢索選自意圖RadioButton值從先前的活動sended。因爲您發送單選價值與gen關鍵,但嘗試使用Extra_gen

+0

幾秒鐘打我。 :)。 +1 – Raghunandan 2013-05-02 04:25:43

+0

嗨,我認爲你忙於工作,自15天以來我沒有看到你。這是好的,你回 – Pragnani 2013-05-02 04:44:16

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.