简体   繁体   English

广告不可见。 android片段中的admob无法刷新问题

[英]ad is not visible. not refreshing issue with admob in android fragments

Am using the following code. 我正在使用以下代码。 Every time on logcat am getting the dialogue ad is not visible. 每次在logcat上获取对话广告都是不可见的。 not refreshing. 不刷新。 And the ad is not showing but the same thing is working fine when am using it with out the help of a fragments. 广告没有展示,但在没有片段帮助的情况下使用同一广告效果很好。

Somebody please tell me whats worng with my code. 有人请告诉我我的代码带来了什么。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    >
     <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        >
     <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                   >
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4"
        android:gravity="center"
        >

                <TextView
                 android:id="@+id/textView1"
                 android:layout_marginLeft="5dp"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 />

                <Button
             android:id="@+id/Button1"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_weight="3"
             android:paddingRight="5dp"
             android:layout_marginRight="15dp"
             android:layout_marginTop="5dp"
             android:layout_marginBottom="5dp"
             android:background="@android:color/transparent"
             />
     </LinearLayout>
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        />

                <TextView
                 android:id="@+id/textView4"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:paddingLeft="5dp"
                 android:paddingRight="5dp"
                 />
                <TextView
                 android:id="@+id/textView2"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:paddingLeft="15dp"
                 android:paddingRight="10dp"
                 android:text=""
                 />
                <TextView
                 android:id="@+id/textView5"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:paddingLeft="5dp"
                 android:paddingRight="5dp"
                 />
                <TextView
                 android:id="@+id/textView3"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:paddingLeft="15dp"
                 android:paddingRight="10dp"
                 />
                <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="xxxxxxxxxxxxxxxxxxxxxxx" />
    </LinearLayout>
    </ScrollView>



</RelativeLayout>

java code Java代码

package com.xx.xx;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

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


public class DetailsFragment extends Fragment {

    public DetailsFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_details, container, false);

        return rootView;
    }

    ListView list;
    Lazyimg adapter;
    String name,imageurl,description,ingradiants,tduration;
    String[] mname,mimageurl;
    private ProgressDialog dialog;
    String ids,b1status;
    Button b1;
    private static Typeface typeFace = null;
    private static Typeface itypeFace = null;


    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        AdView adView = (AdView) getActivity().findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        b1=(Button)getActivity().findViewById(R.id.Button1);

        ids= getArguments().getString("ids");
        ids=ids.replace(" ", "%20");
        ids=ids.replace("-", "%27");

        initTypeFace(getActivity());
        iinitTypeFace(getActivity());
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click  
                GetData obj = new GetData();
                dialog = ProgressDialog.show(getActivity(), "",
                        "Please wait...", true);
                TelephonyManager tManager = (TelephonyManager) getActivity().getBaseContext()
                        .getSystemService(Context.TELEPHONY_SERVICE);
                      String deviceIMEI = tManager.getDeviceId(); 
                String urls="cczczccxx/xxy.php?id="+ids+"&imei="+deviceIMEI+"&fav=jomin";
                obj.execute(urls);
            }
        }); 
        loadingPopup();

    }



    private void loadingPopup() {
        GetData obj = new GetData();
        dialog = ProgressDialog.show(getActivity(), "",
                "Loading recipe details...", true);
        TelephonyManager tManager = (TelephonyManager) getActivity().getBaseContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
              String deviceIMEI = tManager.getDeviceId(); 
        String urls="xxx/xxy.php?id="+ids+"&imei="+deviceIMEI;
        obj.execute(urls);

    }
     public class GetData extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub

                BufferedReader reader =null;
                String data =null;

                try{

                    HttpClient client = new DefaultHttpClient();
                    URI uri=new URI(params[0]);

                    HttpGet get =new HttpGet(uri);

                    HttpResponse response= client.execute(get);

                    InputStream stream=response.getEntity().getContent();

                    reader = new BufferedReader(new InputStreamReader(stream));

                    StringBuffer buffer =new StringBuffer("");
                    String line="";

                    while((line=reader.readLine())!=null){
                        buffer.append(line);
                    }
                    reader.close();
                    data = buffer.toString();

                    JSONObject fulldata = new JSONObject(data);
                    JSONArray albumdata = (JSONArray) fulldata.get("data");
                    JSONObject  sobj = null;

                    name=""; imageurl=""; description=""; ingradiants=""; tduration=""; b1status="";

                    for(int j=0;j<albumdata.length();++j)
                    {
                    sobj= (JSONObject) albumdata.get(j);
                    b1status += (String)sobj.get("fav");
                    name += (String) sobj.get("name");
                    imageurl += (String) sobj.get("imageurl");
                    description += (String) sobj.get("description");
                    ingradiants += (String) sobj.get("ingradiants");
                    tduration += (String) sobj.get("tduration");
                    }

                    return "";

                }
                catch(URISyntaxException e){
                    e.printStackTrace();
                }
                catch(ClientProtocolException f){
                    f.printStackTrace();
                }
                catch(IOException g){
                    g.printStackTrace();
                }
                catch(Exception e)
                {
                //
                }
                finally{
                    if(reader!=null){
                        try{
                            reader.close();
                        }
                        catch(Exception e){

                        }
                    }
                }

                return null;
            }
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);

                try {
                    dialog.dismiss();
                } catch (Exception e) {
                Log.e(e.getClass().getName(), e.getMessage(), e);
                }

                if(result==null)
                {
                    new AlertDialog.Builder(getActivity())
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage("\n    Connection Error..!\n")
                    .setPositiveButton("Exit", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        getActivity().finish();    
                    }

                })
                .setNegativeButton("Retry", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialoga, int which) {
                        try {
                            dialog.dismiss();
                        } catch (Exception e) {
                        Log.e(e.getClass().getName(), e.getMessage(), e);
                        }
                        GetData obj = new GetData();
                        dialog = ProgressDialog.show(getActivity(), "",
                                "Loading recipe details...", true);
                        TelephonyManager tManager = (TelephonyManager) getActivity().getBaseContext()
                                .getSystemService(Context.TELEPHONY_SERVICE);
                              String deviceIMEI = tManager.getDeviceId(); 
                        String urls="xxxxy.php?id="+ids+"&imei="+deviceIMEI;
                        obj.execute(urls);
                    }

                })
                .show();
                }
                else
                {

                mname = name.split("xstream");
                mimageurl = imageurl.split("xstream");

                if(b1status.equals(""))
                {
                    b1.setBackgroundResource(R.drawable.favr);
                }
                else if(b1status.equals("f"))
                {
                    b1.setBackgroundResource(R.drawable.favrg);
                }
                TextView ting=(TextView)getView().findViewById(R.id.textView4);
                ting.setText("Ingredients");
                ting.setTypeface(typeFace);
                ting.setTextColor(Color.parseColor("#210B61"));
                ting.setTextSize(25);

                TextView tpre=(TextView)getView().findViewById(R.id.textView5);
                tpre.setText("\n\nDirections for Preparation");
                tpre.setTypeface(typeFace);
                tpre.setTextColor(Color.parseColor("#210B61"));
                tpre.setTextSize(25);

                if(tduration.equals(""))
                {
                    TextView ttduration=(TextView)getView().findViewById(R.id.textView1);
                    ttduration.setText("READY IN : Depends");
                    ttduration.setTypeface(itypeFace);
                    ttduration.setTextColor(Color.parseColor("#FF8000"));
                    ttduration.setTextSize(20);
                }
                else
                {
                TextView ttduration=(TextView)getView().findViewById(R.id.textView1);
                ttduration.setText("READY IN : "+tduration);
                ttduration.setTypeface(itypeFace);
                ttduration.setTextColor(Color.parseColor("#FF8000"));
                ttduration.setTextSize(20);
                }

                TextView tingradiants=(TextView)getView().findViewById(R.id.textView2);
                ingradiants=ingradiants.replaceAll("<br>", "\n\n");
                ingradiants=ingradiants.replaceAll("&#34;", "\"");
                ingradiants=ingradiants.replaceAll("&#39;", "'");
                ingradiants=ingradiants.replaceAll("&#174;", " ");
                tingradiants.setText(ingradiants);
                tingradiants.setTypeface(typeFace);
                tingradiants.setTextColor(Color.parseColor("#000000"));
                tingradiants.setTextSize(20);


                TextView tdescription=(TextView)getView().findViewById(R.id.textView3);
                description=description.replaceAll("<br>", "\n\n");
                description=description.replaceAll("&#34;", "\"");
                description=description.replaceAll("&#39;", "'");
                description=description.replaceAll("&#174;", " ");

                tdescription.setText(description);
                tdescription.setTypeface(itypeFace);
                tdescription.setTextSize(20);

                list=(ListView)getView().findViewById(R.id.list);
                // Create custom adapter for listview
                adapter=new Lazyimg(getActivity(), mimageurl,mname);

                //Set adapter to listview
                list.setAdapter(adapter);
                }
                                //Button b=(Button)findViewById(R.id.button1);
                //b.setOnClickListener(listener);
            }
        }
     public static void initTypeFace(Context context) {

                try {
                    typeFace = Typeface.createFromAsset(context.getAssets(), "Nexa_Light.otf");
                } catch (Exception e) {
                    e.printStackTrace();
                }

        }
     public static void iinitTypeFace(Context context) {

         try {
             itypeFace = Typeface.createFromAsset(context.getAssets(), "iowan.ttf");
         } catch (Exception e) {
             e.printStackTrace();
         }

 }
}

It could be as simple as your AdView not actually being on screen. 就像您的AdView不在屏幕上一样简单。 The AdView is contained within a ScrollView so there is no guarantee that it is actually on screen. AdView包含在ScrollView中,因此不能保证它确实在屏幕上。

You have a complicated view hierarchy. 您的视图层次结构很复杂。 Suggest you simplify it and move the AdView outside of your ScrollView. 建议您简化一下,然后将AdView移到ScrollView的外面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM