微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我返回包含广告横幅的地图活动时,应用程序崩溃了吗?

如何解决当我返回包含广告横幅的地图活动时,应用程序崩溃了吗?

每当我返回(onResume)到我唯一的活动时,我的应用都会崩溃,并显示以下错误。我的活动仅包含一个Google Map和一个横幅广告(来自Facebook Audience Network)

A/libc: Fatal signal 11 (SIGSEGV),code 1 (SEGV_MAPERR),fault addr 0x0 in tid 14320 (RenderThread),pid 14265

如何解决此问题?非常感谢您的帮助。我的代码很简单,所以我找不到问题。

这是我的activity_maps.xml

<androidx.constraintlayout.widget.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"
    android:id="@+id/constraintLayout"
    tools:context=".MapsActivity">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapsActivity" />

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

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的MapsActivity.java

import androidx.fragment.app.FragmentActivity;

import android.os.Bundle;
import android.widget.LinearLayout;
import com.facebook.ads.AdSize;
import com.facebook.ads.AdView;
import com.facebook.ads.AudienceNetworkAds;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private AdView adView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        AudienceNetworkAds.initialize(this);

        adView = new AdView(this,"my_placement_id",AdSize.BANNER_HEIGHT_50);

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

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

        // Request an ad
        adView.loadAd();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
    }
}

这是它的样子:

enter image description here

解决方法

Logcat message中:

A/libc: Fatal signal 11 (SIGSEGV),code 1 (SEGV_MAPERR),fault addr 0x0 in tid 14320 (RenderThread),pid 14265
  • A/libc显示优先级为A(声明),标签为libcbionic)。
  • 该消息为Fatal signal 11 (SIGSEGV),这意味着您的应用会自行终止,并收到segmentation violation的信号。
  • code 1 (SEGV_MAPERR),fault addr 0x0详细说明libc无法读取或写入存储器地址0x0(空指针),如this answer中所述。
  • 其余in tid 14320 (RenderThread),pid 14265表明此崩溃发生在进程14320 (RenderThread)的线程14265中。

RenderThread表示这可能是由于hardware acceleration造成的。

要关闭加速,请添加属性

android:hardwareAccelerated="false"

AndroidManifest.xml中的其中一个元素。

我认为值得一试。

,

您弄混了一些东西!

在以下位置查看Facebook文档:initialize-the-audience-network-sdk

您可以看到他们在Application类而不是Activity中调用AudienceNetworkAds.initialize(this);

像在文档中那样创建一个类:

public class YourApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize the Audience Network SDK
        AudienceNetworkAds.initialize(this);       
    }

}

下一步是在清单文件中添加带有android:name=".YourApplication"的类,如示例中所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.rieger.com.myapplication">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:name=".YourApplication"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这告诉Android他可以在哪里找到Application类的实现。

然后从MapsActivity类的onCreate()方法中删除AudienceNetworkAds.initialize(this);行。

现在一切正常。

为什么会出现异常?

看看活动生命周期。

Activity Lifecycle

onCreate()可以在每次Activity启动时由系统调用,而不仅仅是onResume()或onStart()。

发生的是,当您通过onCreate()方法第二次打开“活动”时,模拟器调用了onCreate()。导致第二次调用AudienceNetworkAds.initialize(this);,从而导致错误。

另一个提示

不要忘记实现onDestroy()方法,例如:

@Override
protected void onDestroy() {
    if (adView != null) {
        adView.destroy();
    }
    super.onDestroy();
}
,

正如qtmfld所指出的,您的错误很可能发生在RenderThread中。我的推测是,这与您在地图片段上方绘制横幅广告有关。

在xml布局中使用硬编码的片段元素时,尤其是在使用片段事务时,存在已知的绘图问题。因此,在重新创建片段状态时,SupportFragmentManager可能会以某种方式尝试覆盖横幅广告,这会导致崩溃。

除此之外,Google's ad policies反对在用户与之交互的组件上方使用横幅广告。

要确认这一点,您可以尝试更改布局约束,以使横幅广告不会覆盖地图片段:

 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@id/banner_container"
    tools:context=".MapsActivity" />
,

请检查您是否正在使用基于HAXM的仿真器,并检查它是否也出现在实际设备中。

您也可以尝试Google Maps Android SDK v.3.1.0 BETA,看看该错误是否仍然存在。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。