使用单选按钮将活动作为意图传递

时间:2013-10-12 14:48:13

标签: android

我正在尝试将所选单选按钮的文本发送到选择它的下一个

FirstActivity.java

public class FirstActivity extends Activity {

    RadioButton carl;
    RadioButton max;
    RadioGroup RG;

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

        RG=(RadioGroup) findViewById(R.id.RadioButtonGroupID);
        carl=(RadioButton) findViewById(R.id.CarlRadioButtonID);
        max=(RadioButton) findViewById(R.id.MaxRadioButtonID);

        RG.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                Intent INT=new Intent(FirstActivity.this,SecondActivity.class);
                if(checkedId==R.id.CarlRadioButtonID)
                {
                    INT.putExtra("REST", carl.getText());
                }
                else if (checkedId==R.id.MaxRadioButtonID)
                {
                    INT.putExtra("REST", max.getText());
                }
                startActivity(INT);
            }
        });
    }
}

SecondActivity.java

public class SecondActivity extends Activity {

    EditText RecievedText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        String RECIEVEDINTENT=getIntent().getStringExtra("REST");

        RecievedText=(EditText) findViewById(R.id.RecievedText);
        RecievedText.setText(RECIEVEDINTENT);

    }

}

日志::

10-12 20:09:02.846: E/AndroidRuntime(455): FATAL EXCEPTION: main
10-12 20:09:02.846: E/AndroidRuntime(455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radiobutton/com.example.radiobutton.SecondActivity}: java.lang.ClassCastException: android.widget.TextView
10-12 20:09:02.846: E/AndroidRuntime(455):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-12 20:09:02.846: E/AndroidRuntime(455):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-12 20:09:02.846: E/AndroidRuntime(455):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-12 20:09:02.846: E/AndroidRuntime(455):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-12 20:09:02.846: E/AndroidRuntime(455):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 20:09:02.846: E/AndroidRuntime(455):  at android.os.Looper.loop(Looper.java:123)
10-12 20:09:02.846: E/AndroidRuntime(455):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-12 20:09:02.846: E/AndroidRuntime(455):  at java.lang.reflect.Method.invokeNative(Native Method)
10-12 20:09:02.846: E/AndroidRuntime(455):  at java.lang.reflect.Method.invoke(Method.java:507)
10-12 20:09:02.846: E/AndroidRuntime(455):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-12 20:09:02.846: E/AndroidRuntime(455):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-12 20:09:02.846: E/AndroidRuntime(455):  at dalvik.system.NativeStart.main(Native Method)
10-12 20:09:02.846: E/AndroidRuntime(455): Caused by: java.lang.ClassCastException: android.widget.TextView
10-12 20:09:02.846: E/AndroidRuntime(455):  at com.example.radiobutton.SecondActivity.onCreate(SecondActivity.java:18)
10-12 20:09:02.846: E/AndroidRuntime(455):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-12 20:09:02.846: E/AndroidRuntime(455):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

我已宣布清单中的所有活动

我收到了日志错误

如何解决这个问题!

2 个答案:

答案 0 :(得分:0)

Caused by: java.lang.ClassCastException: android.widget.TextView
at com.example.radiobutton.SecondActivity.onCreate(SecondActivity.java:18)

您似乎将您的小部件转换为错误的类型。在TextView中投射或编辑布局中的小部件。

答案 1 :(得分:0)

发现错误.....我曾使用过..... TextView小部件并使用编辑文本进行投射....

更正代码::

public class SecondActivity extends Activity {

    TextView RecievedText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        String RECIEVEDINTENT=getIntent().getStringExtra("REST");

        RecievedText=(TextView) findViewById(R.id.RecievedText);
        RecievedText.setText(RECIEVEDINTENT);

    }

}