简体   繁体   English

如何在扩展ListActivity的类中添加Admob Ads

[英]how to add Admob Ads in a class that extends ListActivity

i want to add java code of Admob Ads in OnCreateMethod. 我想在OnCreateMethod中添加Admob Ads的java代码。 But my java class extends ListActivity. 但我的java类扩展了ListActivity。 Please tell me how to add admob ads in this class. 请告诉我如何在此课程中添加admob广告。

If i add the code in the class that extends ListActivity it crashes the App. 如果我在扩展ListActivity的类中添加代码,它会崩溃App。

Please provide me a solution for this. 请为我提供一个解决方案。

Thanks in Advance. 提前致谢。

my MainActivity java code 我的MainActivity java代码

public class MainActivity extends ListActivity
{
    List headlines;
    List links;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        new MyAsyncTask().execute();
    }

    class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>
    {
        @Override
        protected ArrayAdapter doInBackground(Object[] params)
        {
            headlines = new ArrayList();
            links = new ArrayList();
            try
            {
                URL url = new URL("http://feelthesport.blogspot.com/feeds/posts/default?alt=rss");
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(false);
                XmlPullParser xpp = factory.newPullParser();

                // We will get the XML from an input stream
                xpp.setInput(getInputStream(url), "UTF_8");
                boolean insideItem = false;

                // Returns the type of current event: START_TAG, END_TAG, etc..
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT)
                {
                    if (eventType == XmlPullParser.START_TAG)
                    {
                        if (xpp.getName().equalsIgnoreCase("item"))
                        {
                            insideItem = true;
                        }
                        else if (xpp.getName().equalsIgnoreCase("title"))
                        {
                            if (insideItem)
                                headlines.add(xpp.nextText()); //extract the headline
                        }
                        else if (xpp.getName().equalsIgnoreCase("link"))
                        {
                            if (insideItem)
                                links.add(xpp.nextText()); //extract the link of article
                        }
                    }
                    else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item"))
                    {
                        insideItem=false;
                    }
                    eventType = xpp.next(); //move to next element
                }

            }
            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch (XmlPullParserException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(ArrayAdapter adapter)
        {
            adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, headlines);
            setListAdapter(adapter);
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        Uri uri = Uri.parse((links.get(position)).toString());
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    public InputStream getInputStream(URL url)
    {
        try
        {
            return url.openConnection().getInputStream();
        }
        catch (IOException e)
        {
            return null;
        }
    }
}

I think so rather adding to ListActivity. 我想是这样而是添加到ListActivity。 Add AppCompatActivity & make use of RecyclerView Add Adapter for AdMob Create custom layout whatever you want this could be helpful even if you you want to add your item along with AdMob. 添加AppCompatActivity并使用RecyclerView为AdMob添加适配器即使您要将项目与AdMob一起添加,也可以随意创建自定义布局。 Once added CustomerAdapterClass, call setAdapter() 添加CustomerAdapterClass后,调用setAdapter()

Done your view will get poopulated. 完成你的观点会变得人满为患。 Thanks. 谢谢。

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

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