当我返回到包含广告横幅的Map活动时,java应用程序崩溃了?

enyaitl3  于 2021-07-08  发布在  Java
关注(0)|答案(4)|浏览(404)

每当我回来( onResume )对于我唯一的活动,我的应用程序因以下错误而崩溃。我的活动只包含一个谷歌Map和一个横幅广告(来自facebook受众网络)

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) {
    }
}

下面是它的样子:

slhcrj9b

slhcrj9b1#

正如qtmfld指出的,您的错误很可能发生在renderthread中。我受过教育的猜测是,这与你的横幅广告画上的Map碎片。
在xml布局中使用硬编码的片段元素时,尤其是使用片段事务时,存在已知的绘图问题。因此,有可能supportfragmentmanager在重新创建碎片状态时试图覆盖横幅广告,从而导致崩溃。
除此之外,在用户与之交互的组件上方使用横幅广告是违反谷歌广告政策的。
要确认这一点,您可以尝试更改布局约束,以便横幅广告不会覆盖Map片段:

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

lfapxunr2#

在logcat消息中:

A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 14320 (RenderThread), pid 14265
``` `A/libc` 显示优先级为 `A` (assert)标记为 `libc` (仿生学)。
信息是 `Fatal signal 11 (SIGSEGV)` ,这意味着您的应用程序将自行终止,并接收到违反分段的信号。 `code 1 (SEGV_MAPERR), fault addr 0x0` 细节 `libc` 无法读取或写入内存地址 `0x0` (空指针),如本答案所述。
剩下的 `in tid 14320 (RenderThread), pid 14265` 显示这次崩溃发生在线程中 `14320 (RenderThread)` 工艺流程图 `14265` . `RenderThread` 指示这可能是由于硬件加速造成的。
若要禁用加速度,请添加一个属性

android:hardwareAccelerated="false"

这些元素中的一个 `AndroidManifest.xml` .
整个 `<application />` .
这个 `<activity />` 它崩溃了。
我觉得值得一试。
sqxo8psd

sqxo8psd3#

请检查您是否正在使用 HAXM 并检查它是否也发生在真实的设备上。
你也可以试试googlemapsandroidsdkv.3.1.0beta,看看这个bug是否仍然存在。

cbjzeqam

cbjzeqam4#

你把一些事情搞混了!
看看facebook文档:初始化访问群体网络sdk
你可以看到他们在打电话 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在哪里可以找到应用程序类的实现。
然后拆下 AudienceNetworkAds.initialize(this); 在mapsactivity类中的oncreate()方法中的行。
现在一切都应该顺利了。
为什么会出现异常?
看看活动生命周期。

系统可以在每次启动活动时调用oncreate(),而不仅仅是onresume()或onstart()。
当您通过oncreate()方法第二次打开activity时,emulator会调用oncreate()。这导致了第二次 AudienceNetworkAds.initialize(this); 这导致了错误。
另一个提示
别忘了实现ondestroy()方法,如:

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

相关问题