0

我不断收到错误,我似乎无法修复它。我已经尝试移动代码并在互联网上搜索答案,到目前为止我还没有找到任何答案。请帮助我,我将非常感激。顺便说一句,如果我问了一个愚蠢的问题,我对此很抱歉。我不断收到错误,我似乎无法修复它。

这是我的 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>

我将不胜感激任何帮助,谢谢:)

4

1 回答 1

3

也许你的意思是rg,不是tv

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

您已在 XML 中将这些声明为 TextView:

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

但是您正试图在您的 Java 代码中将这些转换为 RadioGroups:

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

您不能这样做,因为这些类不直接相关......正如您在异常中指出的那样:

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

解决方案很简单:要么您使用了错误的 ID,要么您必须更改您的 XML/Java 代码以使这些匹配。

于 2013-03-14T01:27:32.427 回答