Christian Giupponi
Christian Giupponi

Reputation: 7618

Android admob in FragmentActivity

i have a big problem with my new app, I'm using the new FragmentActivity provided by the new ATD Plugin for Eclipse. The step by step guide has created for me the main FragmentActivity with the 3 section.

This is the code:

package com.italiandevteam.chuck;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;


public class ChuckActivity extends FragmentActivity{


/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
 * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
 * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
 * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chuck);
    // Create the adapter that will return a fragment for each of the three primary sections
    // of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());



    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_chuck, menu);
    return true;
}




/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
 * sections of the app.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return getString(R.string.title_section1);
            case 1: return getString(R.string.title_section2);
            case 2: return getString(R.string.title_section3);
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    public DummySectionFragment() {
    }

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
                //Create the view
        }
}



}

I have insert in the onCreateView my switch to show different layout in this way:

switch(args.getInt(ARG_SECTION_NUMBER)){
                case 1:{
                     myView = getLayoutInflater(null).inflate(R.layout.layout_1, null);
                     break;
                }

                case 2:{
                    myView = getLayoutInflater(null).inflate(R.layout.layout_2, null);
                    break;
                }

            }
return myView

Now i really need to add admob into my project but if i put this coe in the onCreate method

AdView adView;

        // Create the adView
        adView = new AdView(this, AdSize.BANNER, "a14eedc43db2122");

        // Lookup your LinearLayout assuming it’s been given
        // the attribute android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);

        // Add the adView to it
        layout.addView(adView);

        AdRequest request = new AdRequest();
        request.addTestDevice(AdRequest.TEST_EMULATOR);
        request.addTestDevice("B7709F3C1336BFAEFAD6E50877E3211F");

        // Initiate a generic request to load it with an ad
        adView.loadAd(request);

my app will crash and the logcat:

07-11 10:40:21.963: E/AndroidRuntime(4948): FATAL EXCEPTION: main
07-11 10:40:21.963: E/AndroidRuntime(4948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.italiandevteam.chuck/com.italiandevteam.chuck.ChuckActivity}: java.lang.NullPointerException
07-11 10:40:21.963: E/AndroidRuntime(4948):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at android.os.Looper.loop(Looper.java:130)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at android.app.ActivityThread.main(ActivityThread.java:3835)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at java.lang.reflect.Method.invoke(Method.java:507)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at dalvik.system.NativeStart.main(Native Method)
07-11 10:40:21.963: E/AndroidRuntime(4948): Caused by: java.lang.NullPointerException
07-11 10:40:21.963: E/AndroidRuntime(4948):     at com.italiandevteam.chuck.ChuckActivity.onCreate(ChuckActivity.java:66)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-11 10:40:21.963: E/AndroidRuntime(4948):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
07-11 10:40:21.963: E/AndroidRuntime(4948):     ... 11 more

I can't find a way to insert adMob in my project, can you help me?

EDIT:

this is my activity_chuck.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ChuckActivity">

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->
    <android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#000"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />


</android.support.v4.view.ViewPager>

and this is my layout_1 (it's similar to layout_2):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >
    <LinearLayout
       android:id="@+id/linearLayout"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" 
       android:gravity="top"
     >
    </LinearLayout>
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_top" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
<TextView
    android:id="@+id/titolo_scheda"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/biografia"
    android:gravity="center"
    android:textSize="22sp"
    >
    </TextView>
</ScrollView>
</LinearLayout>

EDIT 2: Now with the admob code in the switch it works but just for the first page, if i swipe to the second section i crashed and this is the LogCat:

07-11 14:51:27.117: E/AndroidRuntime(6282): FATAL EXCEPTION: main
07-11 14:51:27.117: E/AndroidRuntime(6282): java.lang.NullPointerException
07-11 14:51:27.117: E/AndroidRuntime(6282):     at com.italiandevteam.chuck.ChuckActivity$DummySectionFragment.onCreateView(ChuckActivity.java:139)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1539)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1422)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.view.View.draw(View.java:6900)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1908)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.view.ViewRoot.draw(ViewRoot.java:1527)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1263)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.os.Looper.loop(Looper.java:130)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at android.app.ActivityThread.main(ActivityThread.java:3835)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at java.lang.reflect.Method.invoke(Method.java:507)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-11 14:51:27.117: E/AndroidRuntime(6282):     at dalvik.system.NativeStart.main(Native Method)

I have put the xml LinearLayout for admob also in the second layout Here layout_2.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_top" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/bryce" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/beckman" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>
    </TableLayout>

    </ScrollView>

      <LinearLayout
       android:id="@+id/linearLayout"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" 
       android:gravity="top"
     >
    </LinearLayout>
</LinearLayout>

Upvotes: 1

Views: 3362

Answers (1)

chrulri
chrulri

Reputation: 1832

Post your layout XMLs too.

Accordingly to your stack trace, findViewById(R.id.pager) returns null. That means this ID is not defined in activity_chuck.xml

Upvotes: 2

Related Questions