gpt4 book ai didi

java - android.widget.textview 无法转换为 android.widget.radiogroup

转载 作者:行者123 更新时间:2023-12-01 23:57:09 25 4
gpt4 key购买 nike

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

这是我的TutorialOne 类(class) -

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>

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

最佳答案

也许您指的是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 代码中将它们转换为 RadioGroup:

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 代码以使它们匹配。

关于java - android.widget.textview 无法转换为 android.widget.radiogroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15399580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com