Pergunta

I did several searches related to AdMob causing Android apps to crash because, well, my app kept crashing. I didn't have any success with it so far. After doing the research, I was able to narrow down the problem a fair amount, so now I'm asking for help to just pass this issue.

Here is the relevant code that I'm using:

MainActivity.java:

public class MainActivity extends ActionBarActivity{    

private AdView adView;
private static final String AD_UNIT_ID = "AD_ID";

@Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }

        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
        layout.addView(adView); 
    }
}

That should be relatively straightforward, right? Now, according to the LogCat, the error occurs on the line layout.addView(adView);. This is where things get confusing for me; I have two layout files for the main activity in my app. The layout file it's referring to in MainActivity.java is activity_main.xml, which actually doesn't have a layout in it. For reference:

activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.fongsoft.appname.MainActivity"
    tools:ignore="MergeRootFrame"
    xmlns:ads="http://schemas.android.com/apk/res-auto" >

</FrameLayout>

The actual layout for the activity is stored in a file called fragment_main.xml, including the LinearLayout that I'm trying to refer to. So it looks like my problem is that I can't seem to find the LinearLayout control because I'm looking in the wrong file. Is there a way to fix this?

If I need to post any additional code or re-word anything, please let me know.

Also, as a side note: elsewhere in MainActivity.java, I seem to be able to refer to controls that are in the LinearLayout in fragment_main.xml. As an example:

public void refreshButtons(){
    Button button1=(Button)findViewById(R.id.button1);
}

In this case, it doesn't make sense to me that I can refer to and manipulate controls within that LinearLayout, but am having trouble adding to it.

Logcat:

 Caused by: java.lang.NullPointerException
     at com.fongsoft.appname.MainActivity.onCreate(MainActivity.java:64)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

Edit: The full LogCat can be found at http://pastebin.com/KhAPgU3r.

Edit #2: After adding in @Ken's suggestions, here's what my current code looks like:

public class MainActivity extends ActionBarActivity{
// Class constructor stuff.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }
}

// ...

public static class PlaceholderFragment extends Fragment{
    private AdView adView;
    private static final String AD_UNIT_ID = "AD_D";


    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;

        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
        layout.addView(adView);
    }   
}

// ...
}

The line LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout); is giving me an error that says, "Cannot make a static reference to the non-static method findViewById(int) from the type Activity." What can I do in this case?

Foi útil?

Solução

After doing more research, I found a solution today that I can work with.

In the fragment_main.xml file, I have a traditional declaration for an AdView control, so I'm not creating the control via code.

In the PlaceholderFragment class, I modified the onCreateView function to read as something like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    AdView adView = (AdView)rootView.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice("TEST_DEVICE")
        .build();
    adView.loadAd(adRequest);

    return rootView;
}

The problem was solved in the line AdView adView = (AdView)rootView.findViewById(R.id.adView);, when I used rootView, which basically allowed me access to fragment_main's content.

Thank you to Ken and a bunch of other people on the internet for helping me figure this out. This problem haunted me for longer than I would have liked.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top