Question

I'm creating an AIR native extension to integrate AdMob.

I've followed the guidelines for using external resources described here: http://www.adobe.com/devnet/air/articles/ane-android-devices.html

and packaged google's adMob jar in my native extension jar as described here: AIR 3 Native Extensions for Android - Can I/How to include 3rd party libraries?

I have a NewAdFunction class which implements FREFunction and is called by the actionscript side. The NewAdFunction.call() function contains essentially the following: (in my actual code I have the necessary try catches)

Intent intent = new Intent(context.getActivity(), AdMobActivity.class);
layoutID = context.getResourceId("layout.adlayout");
intent.putExtra("layoutID", layoutID);
context.getActivity().startActivity(intent);

In the above code adlayout is an xml located in res/layout. I package the entire res folder in my final jar and include the following code in my test AIR project's xml.

<application android:enabled="true" android:debuggable="true">
            <activity android:name="com.mycompany.admob.AdMobActivity"></activity>
</application>

In the following code everything works up until it reaches layout.addView(adView); At this point the program crashes but nothing is printed through the console.

public class AdMobActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutID = getIntent().getIntExtra("layoutID", -1);
        setContentView(layoutID);
        LinearLayout layout = (LinearLayout)findViewById(layoutID);
        AdView adView = new AdView(this, AdSize.BANNER, MY_DEVELOPER_ID);
        layout.addView(adView);
    }
}

All I could think of is that layout could be null, but I added a trace function and it isn't null.

Was it helpful?

Solution

I discovered that actually the layout variable was indeed null. Instead of creating a layout with an xml, I ended up instead creating it programmatically like so:

public FREObject call(FREContext context, FREObject[] args) {
    AdView adView = new AdView(context.getActivity(), AdSize.BANNER, NewAdFunction.developerId);
    Activity activity = context.getActivity();

    ViewGroup viewGroup = (ViewGroup)activity.findViewById(android.R.id.content);
    viewGroup = (ViewGroup)viewGroup.getChildAt(0);
    LinearLayout layout = new LinearLayout(activity);
    viewGroup.addView(layout, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

    adView.setVisibility(View.VISIBLE);
    layout.addView(adView, params);
}

That's the best way I've found to add a view in an AIR native extension.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top