我正在尝试将广告添加到我的应用中,并且我得到了这个奇怪的错误,我无法看到我可能会得到它。这是错误
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): FATAL EXCEPTION: main
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): java.lang.RuntimeException: Unable to start activity ComponentInfo{coderaustin.com/coderaustin.com.Main}: java.lang.ClassCastException: android.widget.EditText
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at android.os.Looper.loop(Looper.java:123)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at java.lang.reflect.Method.invoke(Method.java:521)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at dalvik.system.NativeStart.main(Native Method)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): Caused by: java.lang.ClassCastException: android.widget.EditText
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at coderaustin.com.Main.onCreate(Main.java:79)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-30 20:02:48.889: ERROR/AndroidRuntime(3420): ... 11 more
正如您所看到的,它以某种方式从此代码中获取EditText的实例
AdView adView = new AdView(this, AdSize.BANNER, "my pub code");
View view = findViewById(R.id.RelLayout);
if(view instanceof RelativeLayout) {
Log.e("It's layout", "fixed");
} else {
Log.e("NOOOO", "Instance of: " + view.getClass().getName());
}
**RelativeLayout layout = (RelativeLayout) findViewById(R.id.RelLayout);**
layout.addView(adView);
adView.setGravity(Gravity.BOTTOM);
// Add the adView to it
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
也许这是显而易见的事情,我只是忘了它,我只是没有看到我出错的地方。感谢您的帮助。
编辑:只是想要看看我的布局,这里是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/RelLayout"
>
答案 0 :(得分:2)
我唯一能想到的是你的项目属性(包括ID)需要修复。当您向项目添加新ID /其他属性时,它们有时会失去同步。在eclipse中,您可以右键单击该项目并选择Android&gt;修复项目属性。如果这不起作用,我有时不得不删除所有的.class文件(包括R.class)并重新编译。
希望有所帮助。