2

我在带有 Java 代码的 Android 中的简单应用程序存在一些问题。我正在尝试设置一个类似于按钮颜色设置的 RadioGroup。当我在设置活动 (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

谢谢

4

2 回答 2

3

看来问题出在这一行

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

可能是你试图投一个TextViewto RadioGroup。检查你的 xml。恐怕你的 idButtonSettingsView是 textView

于 2013-05-28T14:01:13.493 回答
1

我认为 LogCat 说明了一切:检查您的 XML 布局文件:ButtonSettingsView实际上是RadioGroup?

于 2013-05-28T14:00:53.960 回答