在ConstrantLayout上添加Facebook观众网络横幅的Eror

时间:2018-02-20 12:14:38

标签: java android android-studio ads facebook-android-sdk

您好我在我的应用程序中添加粉丝横幅时出现问题我收到错误消息 我在我的布局中添加了横幅容器

<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"
    />

然后我实例化广告 - 视图并将其加载到我的活动中

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);

但当我尝试在我的设备上测试代码时,我收到此错误

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

谢谢

1 个答案:

答案 0 :(得分:0)

您的setContentView(R.layout.your_layout)中没有onCreate()。如果不膨胀,布局findViewById将返回null。