请帮本人看看,报错说本人强转这里有问题这是代码:
package com.bb.bb;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// findViewById(R.id.bt_1);
// clickbut();
//获取控件对象
//–Log报错的就是下面这行代码
RadioGroup rg = (RadioGroup) findViewById(R.id.ck1);
// 添加选择改变事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
// 获取 RadioGroup 中的 RadioButton 控件
RadioButton button = (RadioButton) radioGroup.findViewById(i);
// 弹出 RadioButton 的文本信息
Toast.makeText(MainActivity.this, button.getText(), Toast.LENGTH_SHORT).show();
}
});
}
/*private void clickbut() {
findViewById(R.id.bt_1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,”监听1″,Toast.LENGTH_SHORT).show();
}
});
}*/
public void test(View view) {
Toast.makeText(MainActivity.this, “监听5”, Toast.LENGTH_SHORT).show();
}
}
这是Log:
10-16 17:32:04.397 1723-1723/com.bb.bb E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bb.bb, PID: 1723
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bb.bb/com.bb.bb.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatRadioButton cannot be cast to android.widget.RadioGroup
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatRadioButton cannot be cast to android.widget.RadioGroup
at com.bb.bb.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
package com.bb.bb;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// findViewById(R.id.bt_1);
// clickbut();
//获取控件对象
//–Log报错的就是下面这行代码
RadioGroup rg = (RadioGroup) findViewById(R.id.ck1);
// 添加选择改变事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
// 获取 RadioGroup 中的 RadioButton 控件
RadioButton button = (RadioButton) radioGroup.findViewById(i);
// 弹出 RadioButton 的文本信息
Toast.makeText(MainActivity.this, button.getText(), Toast.LENGTH_SHORT).show();
}
});
}
/*private void clickbut() {
findViewById(R.id.bt_1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,”监听1″,Toast.LENGTH_SHORT).show();
}
});
}*/
public void test(View view) {
Toast.makeText(MainActivity.this, “监听5”, Toast.LENGTH_SHORT).show();
}
}
这是Log:
10-16 17:32:04.397 1723-1723/com.bb.bb E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bb.bb, PID: 1723
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bb.bb/com.bb.bb.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatRadioButton cannot be cast to android.widget.RadioGroup
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatRadioButton cannot be cast to android.widget.RadioGroup
at com.bb.bb.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
解决方案
40
你在检查一下xml中,id为ck1的控件是RadioGroup还是RadioButton