2013-03-14 2 views
0

계속 오류가 발생하며 해결할 수 없습니다. 나는 움직이는 코드를 시도하고 답을 찾기 위해 인터넷을 검색했다. 지금까지는 아무 것도 발견하지 못했다. 제발 도와주세요. 정말 감사 할 것입니다. 그런데 im에 관해서는 미안하다. 어리석은 질문을하면. 나는 계속해서 오류가 발생하고 그것을 막을 수 없다. 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 : 당신은 당신의 XML에 TextViews 이러한 선언 한

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

:

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

그러나 당신이 당신의 자바 코드에서 RadioGroups 이러한 캐스팅하려고 :

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

클래스가 di가 아니기 때문에이 작업을 수행 할 수 없습니다.

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

솔루션은 정직 : rectly는 예외로 언급 한 바와 같이 ... 관련 중 잘못된 ID를 사용하거나 이러한 일치를 확인하기 위해 XML/Java 코드를 변경해야합니다.

+0

정말 고마워요, 그럴 수 없다고 생각합니다. 나는 너에게 하나의 빚을지고있다. (: – CrazyCriss

+0

지금은 완벽하게 작동한다. 잘못된 ID를 사용하는 것은 내 잘못이다. – CrazyCriss

+0

도움이되기를 기쁘게 생각한다.이 답을 해결책으로 받아들이려면 체크 표시를 클릭 해주세요. – Sam

관련 문제