我刚遇到这个错误,我不知道它会导致什么......
11-04 14:03:13.170: E/AndroidRuntime(16246): FATAL EXCEPTION: main
11-04 14:03:13.170: E/AndroidRuntime(16246): java.lang.ClassCastException: android.widget.RadioButton cannot be cast to com.google.android.gms.ads.AdView
11-04 14:03:13.170: E/AndroidRuntime(16246): at com.brobox.zahlenraten.MainActivity$ZahlFragment.onCreateView(MainActivity.java:107)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.View.measure(View.java:15525)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.View.measure(View.java:15525)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.View.measure(View.java:15525)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-04 14:03:13.170: E/AndroidRuntime(16246): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2438)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.View.measure(View.java:15525)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.os.Handler.handleCallback(Handler.java:725)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.os.Handler.dispatchMessage(Handler.java:92)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.os.Looper.loop(Looper.java:137)
11-04 14:03:13.170: E/AndroidRuntime(16246): at android.app.ActivityThread.main(ActivityThread.java:5227)
11-04 14:03:13.170: E/AndroidRuntime(16246): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 14:03:13.170: E/AndroidRuntime(16246): at java.lang.reflect.Method.invoke(Method.java:511)
11-04 14:03:13.170: E/AndroidRuntime(16246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-04 14:03:13.170: E/AndroidRuntime(16246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-04 14:03:13.170: E/AndroidRuntime(16246): at dalvik.system.NativeStart.main(Native Method)
所以奇怪的是它说它无法从android.widget.RadioButton转换为com.google.android.gms.ads.AdView但显然我不这样做。
这是我的代码段:
Random randomZahl = new Random();
randomNum = randomZahl.nextInt((max) + 1) + 0;
AdView adView = (AdView) zahlView.findViewById(R.id.adView); //Error line
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("3C7C32E5DD6FF99E1EE5A49DC7E90203").build();
adView.loadAd(adRequest);
以后:
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
// RadioButtonGroup1
case R.id.radio_10:
if (checked) {
max = 10;
Toast.makeText(this, "Rate eine Zahl wischen 0 und 10",
Toast.LENGTH_LONG).show();
}
我希望有人可以帮助我!
答案 0 :(得分:0)
接收各种视图的方法确实收到了adView,并且你总是尝试将视图转换为RadioButton来测试检查状态。您可以将checked =
分配移动到单选按钮的开关盒中,您可以确定该视图是RadioButton类型以防止异常:
// Check which radio button was clicked
switch (view.getId()) {
// RadioButtonGroup1
case R.id.radio_10:
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
if (checked) {
max = 10;
Toast.makeText(this, "Rate eine Zahl wischen 0 und 10",
Toast.LENGTH_LONG).show();
}