[英]App crashes when I return to map activity containing ad banner?
每當我返回 ( 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) {
}
}
它是這樣的:
在Logcat 消息中:
A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 14320 (RenderThread), pid 14265
A/libc
表示優先級為A
(Assert),tag為libc
( bionic )。Fatal signal 11 (SIGSEGV)
,這意味着您的應用程序自行終止,接收到 signal of segmentation violation 。code 1 (SEGV_MAPERR), fault addr 0x0
libc
無法讀取或寫入 memory 地址0x0
(空指針)的詳細信息,如本答案中所述。in tid 14320 (RenderThread), pid 14265
的 rest 表明這次崩潰發生在進程14265
的線程14320 (RenderThread)
中。 RenderThread
表明這可能是由於硬件加速。
要關閉加速,添加一個屬性
android:hardwareAccelerated="false"
到AndroidManifest.xml
中的這些元素之一。
<application />
。<activity />
。我認為值得一試。
你混淆了一些東西!
查看 Facebook 文檔: initialize-the-audience.network-sdk
你可以看到他們調用了AudienceNetworkAds.initialize(this);
在應用程序 class 中而不是在活動中。
像文檔中那樣創建一個 Class:
public class YourApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Initialize the Audience Network SDK
AudienceNetworkAds.initialize(this);
}
}
下一步是在清單文件中添加 class 和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 他可以在哪里找到應用程序 class 的實現。
然后刪除AudienceNetworkAds.initialize(this);
來自 class MapsActivity 中的 onCreate() 方法的行。
現在一切都應該運行良好。
為什么會出現異常?
查看活動生命周期。
onCreate() 可以在您的 Activity 每次啟動時由系統調用,而不僅僅是 onResume() 或 onStart()。
當您通過 onCreate() 方法第二次打開 Activity 時,模擬器會調用 onCreate()。 這導致第二次調用AudienceNetworkAds.initialize(this);
這會導致錯誤。
另一個提示
不要忘記實現 onDestroy() 方法,例如:
@Override
protected void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
正如 qtmfld 指出的那樣,您的錯誤很可能發生在 RenderThread 中。 我有根據的猜測是,它與您在 map 片段之上繪制的橫幅廣告有關。
在 xml 布局中使用硬編碼片段元素時存在已知繪圖問題,尤其是在使用片段事務時。 因此,SupportFragmentManager 可能會在重新創建片段 state 時以某種方式嘗試覆蓋橫幅廣告,這會導致崩潰。
除此之外,在用戶與之交互的組件上方使用橫幅廣告違反了Google 的廣告政策。
要確認這一點,您可以嘗試更改布局約束,使橫幅廣告不會覆蓋地圖片段:
<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
的模擬器,並檢查它是否也發生在真實設備中。
您也可以試試谷歌地圖 Android SDK v.3.1.0 BETA看看這個bug是否還存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.