You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用二次启动崩溃关联AdView布局问题求助

Fixing App Crash on Restart After Exiting AdView-containing Activity

Let's break down your problem and fix it step by step. Your app crashes on second launch after exiting the activity with an AdView, showing two key issues:

  1. "Not enough space to show ad. Needs 360x50 dp, but only has 360x0 dp"
  2. Resources$NotFoundException: Resource ID #0x258

These are linked to your layout setup and improper resource cleanup. Here's how to resolve them:

1. Fix AdView Layout Space Issue

Your current layout has a ListView with layout_height="wrap_content" inside a vertical LinearLayout. When the ListView has enough items, it takes up all available space, pushing the AdView to a 0dp height (hence the ad space error).

Update your layout to guarantee space for the AdView:

<?xml version="1.0" encoding="utf-8"?>
<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.sunplus.suniq.activity.FresherActivity"
    tools:showIn="@layout/activity_fresher">

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- Let ListView take remaining space, leave room for AdView -->
        <ListView
            android:id="@+id/simpleListView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:divider="@color/material_blue_grey_800"
            android:dividerHeight="1dp"
            android:footerDividersEnabled="false" />

        <com.google.android.gms.ads.AdView 
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>
  • Using layout_height="0dp" + layout_weight="1" on the ListView makes it occupy all space except what's needed for the AdView.
  • Setting layout_width="match_parent" on the AdView ensures it fits the screen width correctly for the BANNER ad size.

2. Clean Up Resources on Activity Destroy

When exiting the activity, failing to properly destroy the AdView or clear the ListView adapter can cause memory leaks and resource conflicts on restart. Add this to your FresherActivity:

@Override
protected void onDestroy() {
    // Clear ListView adapter to avoid invalid context references
    if (simpleListView != null) {
        simpleListView.setAdapter(null);
    }
    // Destroy AdView to release ad resources
    if (adView != null) {
        adView.destroy();
    }
    super.onDestroy();
}

3. Fix Resources$NotFoundException

The log points to CustomAdapter.getView() line 80 where an invalid resource ID (0x258 = 600) is being used. Check this code:

  • Ensure you're using valid drawable resource IDs (not hardcoded numbers like 600).
  • Use a WeakReference for the Context in your adapter to prevent memory leaks that can cause resource access issues:
public class CustomAdapter extends BaseAdapter {
    private final WeakReference<Context> contextRef;

    public CustomAdapter(Context context) {
        this.contextRef = new WeakReference<>(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Context context = contextRef.get();
        if (context == null) {
            return convertView;
        }
        // Rest of your getView logic...
        // Example: Use a valid resource ID, not a random number
        // imageView.setImageResource(R.drawable.your_valid_image);
    }
}

4. Test the Fixes

  1. Exit the AdView-containing activity normally.
  2. Restart the app to verify the crash no longer occurs.
  3. Confirm the ad loads without space errors, and all list items display correctly without resource exceptions.

内容的提问来源于stack exchange,提问作者A Koul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:04:18