簡體   English   中英

廣告不可見。 android片段中的admob無法刷新問題

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

我正在使用以下代碼。 每次在logcat上獲取對話廣告都是不可見的。 不刷新。 廣告沒有展示,但在沒有片段幫助的情況下使用同一廣告效果很好。

有人請告訴我我的代碼帶來了什么。

<?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代碼

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();
         }

 }
}

就像您的AdView不在屏幕上一樣簡單。 AdView包含在ScrollView中,因此不能保證它確實在屏幕上。

您的視圖層次結構很復雜。 建議您簡化一下,然后將AdView移到ScrollView的外面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM