开发者

Android: Trying to add admod to a second activity that I start with an intent

开发者 https://www.devze.com 2023-03-28 07:22 出处:网络
I have an application that has a start up screen and when the user clicks a button it starts the real app in a new activity using an intent.When I try to add admob adds to the second activity the app

I have an application that has a start up screen and when the user clicks a button it starts the real app in a new activity using an intent. When I try to add admob adds to the second activity the app just crashes.

I took a sample app off admobs web site and modified it to replicate what I am doing. I get the same results. The adds work on the first actiivity, but when I start the second activity it just crashes.

Here is my code

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.google.ads.example"
  android:versionCode="1"
  android:versionName="1.0">
<application android:icon="@drawable/icon" android:la开发者_如何学编程bel="@string/app_name">
    <activity android:name="BannerXML"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.google.ads.AdActivity" 
              android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
</manifest> 

Main XML from original sample code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/res/com.google.ads.example"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

<Button android:text="Button"
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>              

<com.google.ads.AdView android:id="@+id/adView"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       ads:adUnitId="MY_AD_UNIT_ID"
                       ads:adSize="BANNER" />

</LinearLayout>

Original Source code with my added code to start new activity

public class BannerXML extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  final Button myBtn = (Button) findViewById(R.id.button1);
  myBtn.setOnClickListener(new Button.OnClickListener() {  
        @Override
        public void onClick(View v) {
            start_new_activity();
        }
       });

  // Look up the AdView as a resource and load a request.
  AdView adView = (AdView)this.findViewById(R.id.adView);
  adView.loadAd(new AdRequest());
}


private void start_new_activity()
{
     Intent i = new Intent(BannerXML.this, test_add.class);
    this.startActivity(i);  
    finish();
}
}  

My new activity class

public class test_add extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.test);
    AdView adView = (AdView)this.findViewById(R.id.adView1);
    adView.loadAd(new AdRequest()); 
}
}

My XML for second layout activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<TextView android:text="This is a test" 
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
 </TextView>
 <com.google.ads.AdView android:id="@+id/adView1"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       ads:adUnitId="a14e449d41a7dbe"
                       ads:adSize="BANNER" />
</LinearLayout>

The problem seems to be something with the XML for my new layout. Even if I never do an add request in the second activity it still crashes when it starts

Any Help Would Be Appreciated Thanks


In your Manifest, there is no declaration for the test_add activity..

Also I suggest respecting the CamelCase convention for naming Java classes

0

精彩评论

暂无评论...
验证码 换一张
取 消