【问题标题】:When I put RadioButton There is Error in Android Studio当我放 RadioButton 时 Android Studio 出现错误
【发布时间】:2016-07-14 01:06:50
【问题描述】:

我有问题。我无法在我的项目中添加单选按钮。

我的 Xml 代码:

<?xml version="1.0" encoding="utf-8"?>

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton"
    android:id="@+id/radioButton"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="183dp" />

我的 Java 代码:

public class MainActivity extends Activity {
RadioButton rb;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 rb = (RadioButton)findViewById(R.id.radioButton);

}

}

我的聊天记录:

03-26 10:13:10.681 12433-12433/? D/dalvikvm: Late-enabling CheckJNI
03-26 10:13:10.777 12433-12433/? D/AndroidRuntime: Shutting down VM
03-26 10:13:10.777 12433-12433/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa6249288)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime: FATAL EXCEPTION: main
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wesley.radiobuttonuygulama/com.example.wesley.radiobuttonuygulama.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.RadioButton
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.RadioButton
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.Activity.setContentView(Activity.java:1867)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.example.wesley.radiobuttonuygulama.MainActivity.onCreate(MainActivity.java:11)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5008)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at java.lang.reflect.Constructor.constructNative(Native Method)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.Activity.setContentView(Activity.java:1867) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.example.wesley.radiobuttonuygulama.MainActivity.onCreate(MainActivity.java:11) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5008) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
03-26 10:13:10.941 12433-12433/com.example.wesley.radiobuttonuygulama E/AndroidRuntime:  Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_btn_radio_material.xml from drawable resource ID #0x7f020008

【问题讨论】:

  • 你的 xml 中的第 17 行是什么?
  • 您是否只是在 xml 中放置了一个单选按钮,而没有将其包含在布局中?发给你完整的xml代码

标签: android button radio


【解决方案1】:

从android的widget包中导入单选按钮:

import android.widget.RadioButton;
import com.example.wesley.radiobuttonuygulama.R;

[com.example.wesley.radiobuttonuygulama] 是你的应用包名

【讨论】:

  • 将您的单选按钮 ID 更改为“my_radio_btn”之类的东西,这很有趣,但我在您的代码中找不到任何问题
  • 你是否导入了你项目的 R 类?再次查看我的答案
  • 您的项目有错误,因为需要 R 类才能从您的布局中找到视图 id 的索引...重建它我应该做我的功课现在我稍后回来:D 希望你解决你的问题直到我回来……愿原力与你同在:D
【解决方案2】:

我认为这可能对您有所帮助,因为在当前代码中它只是初始化但未使用,所以我认为这个示例会有所帮助

     RadioGroup Class; // declared radio group
     RadioButton selectclass;

   @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    radioListener(); 
    }

     public void radioListener(){

        Class = (RadioGroup)findViewById(R.id.radioGroup1);
        select = (Button)findViewById(R.id.submitbut);

        select.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

             getSelected = Class.getCheckedRadioButtonId();

             selectclass = (RadioButton)findViewById(getSelected);

                 Toast.makeText(MainActivity.this,
       selectclass.getText().toString(),
            Toast.LENGTH_SHORT).show();

                }
            });

http://androidcoding.in/2016/02/22/android-tutorial-on-radio-button/

【讨论】:

  • 请添加更多信息 - 请记住,尽管链接断开,但答案必须有效!
  • 您应该为链接获取损坏的情况添加一些示例!
  • 没问题,谢谢!
猜你喜欢
  • 2013-05-20
  • 2023-03-14
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 2016-05-06
  • 2020-02-01
  • 1970-01-01
相关资源
最近更新 更多