2013-03-14 61 views
0

我不斷收到一個錯誤,我似乎無法修復它。 我已經嘗試了移動代碼並在互聯網上搜索答案,到目前爲止我還沒有找到任何答案。請幫助我,我會非常感激。順便說一句,如果我提出一個愚蠢的問題,我很抱歉。我不斷收到一個錯誤,我似乎無法修復它。android.widget.textview不能轉換爲android.widget.radiogroup

這是我TutorialOne類 -

package com.chicas.thebasics; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.view.Gravity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 

public class tutorialOne extends Activity implements OnCheckedChangeListener{ 

    public TextView textOut; 
    public EditText textIn; 
    public RadioGroup gravityG; 
    public RadioGroup styleG; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tutorial1); 
     textOut = (TextView) findViewById(R.id.tvChange); 
     textIn = (EditText) findViewById(R.id.editText1); 
     gravityG = (RadioGroup) findViewById(R.id.tvGravity); 
     styleG = (RadioGroup) findViewById(R.id.tvStyle); 
     Button gen = (Button) findViewById(R.id.bGenerate); 
     gen.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
      textOut.setText(textIn.getText()); 
      } 

    }); 

} 

public void onCheckedChanged(RadioGroup group, int checkedId) { 
    // TODO Auto-generated method stub 

    switch(checkedId){ 
    case R.id.rbLeft: 
     textOut.setGravity(Gravity.LEFT); 
     break; 
    case R.id.rbCenter: 
     textOut.setGravity(Gravity.CENTER); 
     break; 
    case R.id.rbRight: 
    textOut.setGravity(Gravity.RIGHT); 
     break; 

    } 

} 


} 

這是錯誤日誌

03-13 18:02:34.773: E/AndroidRuntime(677): FATAL EXCEPTION: main 
03-13 18:02:34.773: E/AndroidRuntime(677): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.chicas.thebasics/com.chicas.thebasics.tutorialOne}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioGroup 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.app.ActivityThread.access$600(ActivityThread.java:130) 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.os.Looper.loop(Looper.java:137) 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.app.ActivityThread.main(ActivityThread.java:4745) 
03-13 18:02:34.773: E/AndroidRuntime(677): at java.lang.reflect.Method.invokeNative(Native Method) 
03-13 18:02:34.773: E/AndroidRuntime(677): at java.lang.reflect.Method.invoke(Method.java:511) 
03-13 18:02:34.773: E/AndroidRuntime(677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
03-13 18:02:34.773: E/AndroidRuntime(677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-13 18:02:34.773: E/AndroidRuntime(677): at dalvik.system.NativeStart.main(Native Method) 
03-13 18:02:34.773: E/AndroidRuntime(677): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioGroup 
03-13 18:02:34.773: E/AndroidRuntime(677): at com.chicas.thebasics.tutorialOne.onCreate(tutorialOne.java:28) 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.app.Activity.performCreate(Activity.java:5008) 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
03-13 18:02:34.773: E/AndroidRuntime(677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
03-13 18:02:34.773: E/AndroidRuntime(677): ... 11 more 
03-13 18:07:34.853: I/Process(677): Sending signal. PID: 677 SIG: 9 

現在,這是我的tutorial1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:text="" > 
</EditText> 


<LinearLayout 
android:layout_width="match_parent" 
android:weightSum="2" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

<TextView 
    android:id="@+id/tvStyle" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Style" 
    android:gravity="center" 
    android:textStyle="bold" 
    /> 

<TextView 
    android:id="@+id/tvGravity" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Gravity" 
    android:gravity="center" 
    android:textStyle="bold" 
    /> 

</LinearLayout> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<RadioGroup 
android:id="@+id/rgStyle" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1"> 

<RadioButton 
    android:id="@+id/rbNormal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Normal" /> 

<RadioButton 
    android:id="@+id/rbItalic" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Italic" /> 

<RadioButton 
    android:id="@+id/rbBold" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Bold" /> 

</RadioGroup> 

<RadioGroup 
android:orientation="vertical" 
android:id="@+id/rgGravity" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1"> 


<RadioButton 
    android:id="@+id/rbLeft" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Left" /> 

<RadioButton 
    android:id="@+id/rbCenter" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Center" /> 

<RadioButton 
    android:id="@+id/rbRight" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Right" /> 
</RadioGroup> 




</LinearLayout> 


<TextView 
    android:id="@+id/tvChange" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="25dp" 
    android:text="Type in Text and press the Button Bellow" 
    android:gravity="center"/> 


<Button 
    android:id="@+id/bGenerate" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Generate" /> 

</LinearLayout> 

我將感謝任何幫助,謝謝:)

回答

3

也許你的意思rg,不tv

gravityG = (RadioGroup) findViewById(R.id.rgGravity); 
styleG = (RadioGroup) findViewById(R.id.rgStyle); 

你已經把這些作爲你的XML TextViews:

<TextView 
    android:id="@+id/tvStyle" 
... 
<TextView 
    android:id="@+id/tvGravity" 

但你嘗試投放這些在你的Java代碼RadioGroups:

gravityG = (RadioGroup) findViewById(R.id.tvGravity); 
styleG = (RadioGroup) findViewById(R.id.tvStyle); 

你不能這樣做,因爲類不是di rectly相關...你由異常指出:

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioGroup 

解決方法是直截了當:要麼你使用了錯誤的ID或你必須改變你的XML/Java代碼,使這些比賽。

+0

非常感謝你,我不相信我沒有抓住那個哈哈哈。我欠你一個(: – CrazyCriss 2013-03-14 01:43:44

+0

它現在工作完美,這是我的錯誤使用錯誤的ID – CrazyCriss 2013-03-14 01:47:21

+0

很高興我可以幫助,請點擊複選標記接受這個答案作爲解決方案。 – Sam 2013-03-14 14:05:37

相關問題