无法正确放置admob广告

时间:2017-08-26 16:41:45

标签: java android admob

我正在学习修改我在线购买的Android应用程序源代码。在某些布局上已经有AdMob广告,但我想将它们添加到另一个版面。我所做的就是在相关文件中复制所需的代码位。 /> 这是修改后的fragment_category.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:id="@+id/main_layout"
    tools:context=".CategoryFragment">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container_body" >


    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#00ffffff"
            android:gravity="center"
            android:id="@+id/container_admob"
            android:visibility="gone">

            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                ads:adSize="BANNER"
                ads:adUnitId="@string/banner_ad_unit_id">
            </com.google.android.gms.ads.AdView>



        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/container_items"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@color/white">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:scrollbars="vertical" />

        </android.support.v4.widget.SwipeRefreshLayout>



    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/message"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

修改了CategoryFragment.java file.only相关位(full CategoryFragment.java file

public class CategoryFragment extends Fragment implements Constants, SwipeRefreshLayout.OnRefreshListener {

    private static final String STATE_LIST = "State Adapter Data";

    RecyclerView mRecyclerView;
    TextView mMessage;
    LinearLayout mContainerAdmob;

    SwipeRefreshLayout mItemsContainer;

    private ArrayList<Item> itemsList;
    private FeedListAdapter itemsAdapter;

    private int categoryId = 0;

    private int itemId = 0;
    private int arrayLength = 0;
    private Boolean loadingMore = false;
    private Boolean viewMore = false;
    private Boolean restore = false;

    private String title = "";

    int pastVisiblesItems = 0, visibleItemCount = 0, totalItemCount = 0;

    public CategoryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mContainerAdmob = (LinearLayout) findViewById(R.id.container_admob);

        if (App.getInstance().getAdmob() == ADMOB_ENABLED) {

            mContainerAdmob.setVisibility(View.VISIBLE);

            AdView mAdView = (AdView) findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
        }
.java文件中的

findViewById为红色[错误 - 无法解析方法findViewById(int)] fragment_java.xml文件未显示任何错误

工作文件

  1. activity_main.xml
  2. MainActivity.java
  3. 我添加了什么,

    在fragment_category.xml

    xmlns:ads="http://schemas.android.com/apk/res-auto"

    <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#00ffffff"
                android:gravity="center"
                android:id="@+id/container_admob"
                android:visibility="gone">
    
                <com.google.android.gms.ads.AdView
                    android:id="@+id/adView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_alignParentBottom="true"
                    ads:adSize="BANNER"
                    ads:adUnitId="@string/banner_ad_unit_id">
                </com.google.android.gms.ads.AdView>
    
    在CategoryFragment.java中

    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;
    

    LinearLayout mContainerAdmob;

    mContainerAdmob = (LinearLayout) findViewById(R.id.container_admob);
    
            if (App.getInstance().getAdmob() == ADMOB_ENABLED) {
    
                mContainerAdmob.setVisibility(View.VISIBLE);
    
                AdView mAdView = (AdView) findViewById(R.id.adView);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            }
    

    如果所有这些都太乱了,请建议我这样做。我尝试过教程,但每个都有不同的代码

2 个答案:

答案 0 :(得分:0)

  

仅相关位

你切断了onCreateView。这是您必须使用rootView.findViewById获取广告和mContainerAdmob

的方法

答案 1 :(得分:0)

您无法在片段中实现活动方法。你必须使用片段的onCreateview()方法并膨胀它的布局然后可以调用findViewById();

实施的最佳方式

public View onCreateView(LayoutInflater inflater, 
ViewGroup container, Bundle savedInstanceState) { 
rootView = inflater.inflate(R.layout.fragment_category, 
container, false);
AdView mAdView =
(AdView)rootView.findViewById(R.id.adView);
}

通过这种方式,rootView可以用于xml布局中定义的每个id。

希望这有帮助。