Admob不会显示webview

时间:2013-08-29 06:38:23

标签: android webview admob

我无法显示我的admob广告..我认为问题出在setContentView(web)上,但我无法弄清楚如何修复它。我认为这是因为我切换到不同的布局?我试过搜索但找不到解决方案

public class MainMenu extends Activity {
WebView web;
ProgressBar progressBar;
private AdView adView;

private ValueCallback<Uri> mUploadMessage;  
 private final static int FILECHOOSER_RESULTCODE=1;  

 @Override  
 protected void onActivityResult(int requestCode, int resultCode,  
                                    Intent intent) {  
  if(requestCode==FILECHOOSER_RESULTCODE)  
  {  
   if (null == mUploadMessage) return;  
            Uri result = intent == null || resultCode != RESULT_OK ? null  
                    : intent.getData();  
            mUploadMessage.onReceiveValue(result);  
            mUploadMessage = null;  
  }
  }  

/** Called when the activity is first created. */ 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Parse.initialize(this, "", "");
    ParseAnalytics.trackAppOpened(getIntent());
    ParseObject testObject = new ParseObject("TestObject");
    testObject.put("foo", "bar");
    testObject.saveInBackground();
    //Parse.initialize(this, "", ""); 
    //ParseObject testObject = new ParseObject("TestObject");
    //testObject.put("foo", "bar");
    //testObject.saveInBackground();
    setContentView(R.layout.mainmenu);


    // Create an ad.
    adView = new AdView(this, AdSize.BANNER, ""); 

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
  AdRequest adRequest = new AdRequest();
  adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
  adRequest.addTestDevice("");    

    // Start loading the ad in the background.
    adView.loadAd(adRequest);


    web = (WebView) findViewById(R.id.webView1);
    //progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    web = new WebView(this);  
    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setLoadWithOverviewMode(true);
    web.getSettings().setUseWideViewPort(true);
    web.loadUrl("http://.com"); 
    web.getSettings().setDefaultZoom(ZoomDensity.FAR);
    web.setWebViewClient(new WebViewClient()); 
    web.setWebChromeClient(new WebChromeClient()  
    {  
           //The undocumented magic method override  
           //Eclipse will swear at you if you try to put @Override here  
        // For Android 3.0+
        @SuppressLint("NewApi")
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {  
            File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Modeling Hawaii");
           // Create the storage directory if it does not exist
           if (! imageStorageDir.exists()){
               imageStorageDir.mkdirs();                  
           }
           File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");  
           Uri imageUri = Uri.fromFile(file);  

           final List<Intent> cameraIntents = new ArrayList<Intent>();
           final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
           final PackageManager packageManager = getPackageManager();
           final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
           for(ResolveInfo res : listCam) {
               final String packageName = res.activityInfo.packageName;
               final Intent intent = new Intent(captureIntent);
               intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
               intent.setPackage(packageName);
               intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
               cameraIntents.add(intent);
           }

            mUploadMessage = uploadMsg;  
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
            i.addCategory(Intent.CATEGORY_OPENABLE);  
            i.setType("image/*");  
            MainMenu.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);  

           }

        // For Android 3.0+
           public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
           mUploadMessage = uploadMsg;
           Intent i = new Intent(Intent.ACTION_GET_CONTENT);
           i.addCategory(Intent.CATEGORY_OPENABLE);
           i.setType("*/*");
           MainMenu.this.startActivityForResult(
           Intent.createChooser(i, "File Browser"),
           FILECHOOSER_RESULTCODE);
           }

        //For Android 4.1
           public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
               mUploadMessage = uploadMsg;  
               Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
               i.addCategory(Intent.CATEGORY_OPENABLE);  
               i.setType("image/*");  
               MainMenu.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainMenu.FILECHOOSER_RESULTCODE );

           }

    });  


    setContentView(web);  






}

class MyWebViewClient extends WebViewClient {
      @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

           // progressBar.setVisibility(View.GONE);
        }
}   
  //flipscreen not loading again
    @Override
    public void onConfigurationChanged(Configuration newConfig){        
        super.onConfigurationChanged(newConfig);
    }
    //To handle "Back" key press event for WebView to go back to previous screen.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
     if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
         web.goBack();
         return true;
     }
 return super.onKeyDown(keyCode, event);
}
@Override
public void onDestroy() {
  if (adView != null) {
    adView.destroy();
  }
  super.onDestroy();
}


//    public void onBackPressed() 
//    {
//        if(mWebView.canGoBack())
//            mWebView.goBack();
//        else{
//            Intent start = new Intent(MainMenu.this,MainMenu.class);
//        startActivity(start);
//        finish();    }}



}

1 个答案:

答案 0 :(得分:0)

将您的WebView封装在另一个ViewGroup中,并将AdView放入其中。然后,您可以使用AdView更改contentView。实际上,当您将活动的contentView设置为Web视图或另一个View时,它将仅显示视图。因此,您最好将内容视图设置为ViewGroup LinearLayout,并将您的观点添加到其中。

我希望这对你有所帮助。