gpt4 book ai didi

java - 广播组崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 08:04:08 25 4
gpt4 key购买 nike

我在使用 Java 代码的 Android 中使用我的简单应用程序时遇到了一些问题。我正在尝试设置一个类似于按钮颜色设置的 RadioGroup。当我在设置 Activity (Settings.java) 中启动我的应用程序时,它崩溃了。

package com.app.testing;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Settings extends Main implements OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.ButtonSettingsView);

int checkedRadioButton = radioGroup.getCheckedRadioButtonId();

switch (checkedRadioButton) {
case R.id.redbtn :
add.setBackgroundColor(21);
break;
case R.id.blubtn :
add.setBackgroundColor(58);
break;
case R.id.grebtn :
add.setBackgroundColor(13);
break;
}

Button back = (Button) findViewById(R.id.back);

back.setOnClickListener(new View.OnClickListener() {

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

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
}
}

日志:

05-27 16:27:49.611: E/AndroidRuntime(4970): FATAL EXCEPTION: main
05-27 16:27:49.611: E/AndroidRuntime(4970): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.testing/com.app.testing.Settings}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioGroup
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.os.Looper.loop(Looper.java:137)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-27 16:27:49.611: E/AndroidRuntime(4970): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 16:27:49.611: E/AndroidRuntime(4970): at java.lang.reflect.Method.invoke(Method.java:511)
05-27 16:27:49.611: E/AndroidRuntime(4970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-27 16:27:49.611: E/AndroidRuntime(4970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-27 16:27:49.611: E/AndroidRuntime(4970): at dalvik.system.NativeStart.main(Native Method)
05-27 16:27:49.611: E/AndroidRuntime(4970): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioGroup
05-27 16:27:49.611: E/AndroidRuntime(4970): at com.app.testing.Settings.onCreate(Settings.java:16)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.Activity.performCreate(Activity.java:5104)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-27 16:27:49.611: E/AndroidRuntime(4970): ... 11 more

谢谢

最佳答案

看来问题出在这一行

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.ButtonSettingsView); 

可能您正在尝试将 TextView 转换到 RadioGroup。检查你的xml。恐怕你的 id ButtonSettingsView 是一个 textView

关于java - 广播组崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16794031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com