有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我是否正确地实现了广告,为什么没有显示?

横幅不希望以任何方式显示(

我创建了一个单独的Ads.java文件

在Gridle和Android中,清单正确地输入了所有内容

位于路径沿线:com。我的项目。应用程序。活动Ads.java

import 安卓.os.Bundle;

import 安卓x.appcompat.app.AppCompatActivity;

import com.google.安卓.gms.ads.AdRequest;
import com.google.安卓.gms.ads.AdView;
import com.google.安卓.gms.ads.MobileAds;
import com.google.安卓.gms.ads.initialization.InitializationStatus;
import com.google.安卓.gms.ads.initialization.OnInitializationCompleteListener;
import com.myproject.app.app.R;

public class Ads extends AppCompatActivity {
    private AdView mAdView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

试着把你带回家。xml中的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:id="@+id/home_root"
    安卓:orientation="vertical">

    <TextView
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:id="@+id/tv_cat_title"
        安卓:text="@string/collections"
        安卓:textStyle="bold"
        安卓:textSize="@dimen/text_16sp"
        安卓:layout_margin="@dimen/margin_5dp" />

    <com.google.安卓.gms.ads.AdView
        安卓:id="@+id/adView"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.安卓.gms.ads.AdView>


    <安卓x.recyclerview.widget.RecyclerView
        安卓:id="@+id/category_list"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent" />
</LinearLayout>

我的家庭碎片。他们将从哪里回家。xml 我必须在里面写广告,或者不一定,或者在另一个文件里

import 安卓.content.Intent;
import 安卓.os.Bundle;

import 安卓x.annotation.NonNull;
import 安卓x.annotation.Nullable;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓x.fragment.app.Fragment;
import 安卓x.recyclerview.widget.DefaultItemAnimator;
import 安卓x.recyclerview.widget.LinearLayoutManager;
import 安卓x.recyclerview.widget.RecyclerView;



import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.LinearLayout;
import 安卓.widget.Toast;


import  com.myproject.app.app.R;
import com.myproject.app.app.activities.SecondActivity;
import com.myproject.app.app.utils.CategoryItem;
import com.myproject.app.app.utils.NetworkUtils;
import com.myproject.app.app.utils.RecyclerAdapter;
import com.myproject.app.app.utils.StaticUtils;

import java.util.ArrayList;

import com.myproject.app.app.Ads;


public class HomeFragment extends Fragment implements RecyclerAdapter.OnItemClickListener {
    private static final String TAG = "HomeFragment";

    private RecyclerView mRecyclerView;
    private RecyclerAdapter adapter;
    private View root;
    private AdView mAdView;



    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: making the layout visible");
        return inflater.inflate(R.layout.fragment_home, container, false);


    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onViewCreated: view created successfully");
        super.onViewCreated(view, savedInstanceState);

        mRecyclerView = view.findViewById(R.id.category_list);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemViewCacheSize(20);
        mRecyclerView.setDrawingCacheEnabled(true);
        mRecyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        root = view.findViewById(R.id.home_root);
        //setting adapter
        adapter = new RecyclerAdapter(getContext(),root, StaticUtils.categoryList, StaticUtils.imagesList,0);  //adapter attached
        adapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(adapter);



    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: gets called.");
        super.onCreate(savedInstanceState);


        //getting the lists ready
        StaticUtils.categoryList = new ArrayList<>();
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.featured,"Featured"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.abstruct,"Abstract"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.army,"Army"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.baby,"Baby"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.beach,"Beach"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.cars,"Cars"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.city,"City"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.foods,"Foods"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.girls,"Girls"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.macro,"Macro"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.nature,"Nature"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.religious,"Religious"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.sports,"Sports"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.stars,"Stars"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.technology,"Technology"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.water,"Water"));
        StaticUtils.categoryList.add(new CategoryItem(R.drawable.wildlife,"Wildlife"));
    }


    @Override
    public void onItemClick(int position) {
        Log.d(TAG, "onItemClick: clicked item");
        NetworkUtils network = new NetworkUtils(getContext());

        if(network.checkConnection(root)){      //if network connected
            Intent i = new Intent(getActivity(), SecondActivity.class);
            i.putExtra(StaticUtils.KEY_FRAG_ID,0);
            if (position!=0){
                i.putExtra(StaticUtils.KEY_SEARCH_DATA,StaticUtils.categoryList.get(position).getmTitle());
            }
            startActivity(i);
        }
    }

}

共 (1) 个答案

  1. # 1 楼答案

    在mainfest中添加这一行。允许应用程序使用internet的xml文件

      <uses-permission android:name="android.permission.INTERNET" />