2013-05-28 90 views
2

我在使用Java代碼的Android中使用我的簡單應用程序時遇到了一些問題。我試圖設置一個RadioGroup,它像按鈕顏色的設置一樣工作。當我在設置活動(Settings.java)中啓動我的應用程序時,它崩潰。RadioGroup Crash

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 

感謝

+0

發佈你的佈局xml文件 – Pragnani

回答

3

看來問題是在這條線

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

可能是你正在努力蒙上了TextViewRadioGroup。檢查你的xml。我怕你的編號ButtonSettingsView是textView

+0

這是正確的我爲RadioGroup使用TextView id謝謝! – arabello

1

我覺得logcat的說,這一切: 檢查你的XML佈局文件:是ButtonSettingsView實際上是一個RadioGroup