I am trying to add ads into my app, and I get this weird error that I just can’t see how I am possibly getting it. Here is the error
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
As you can see, it’s somehow getting an instance of EditText out of this code
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());
Maybe it’s something obvious and I am just oblivious to it, I just don’t see where I’m going wrong. Thanks for all help.
Edit: Just incase you wanted to see my layout, here it is
<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"
>
The only thing I can think of is that your project properties (which includes IDs) need fixing. They get out of sync sometimes when you add new IDs/other properties to the project. In eclipse you can right click on the project and select Android > Fix Project Properties. If that doesn’t work I have sometimes had to delete all of my .class files (including R.class) and re-compile.
Hope that helps.