简体   繁体   中英

Eror adding facebook audience network banner on ConstrantLayout

Hello there i have a problem adding a fan banner in my application i get a error i'v added the banner container in my layout

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kimali.bhirod.MainActivity">

<LinearLayout
    android:id="@+id/banner_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    />

then i instantiate the Ad - View and load it in my activity

public class MainActivity extends AppCompatActivity {

private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Instantiate an AdView view
    adView = new AdView(this, "153676755260608_153679028593714", AdSize.BANNER_HEIGHT_50);

    // Find the Ad Container
    LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);

    // Add the ad view to your activity layout
    adContainer.addView(adView);

    // Request an ad
    adView.loadAd(); setContentView(R.layout.activity_main);

but when i tried to test the code on my device i get this error saying

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kimali.bhirod/com.kimali.bhirod.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference

thanks

You dont have setContentView(R.layout.your_layout) in your onCreate() . Without inflating the layout findViewById will return null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM