2012-12-08 7 views
2

앱에 Admob 광고를 구현하려고합니다. 내가 원하는 것은 사용자 정의 대화 상자에 배너 광고를 보내는 것입니다. 나는 모든 것을 시도했지만 해결책을 찾을 수 없습니다.Admob 광고 맞춤 설정 대화 상자

대화 상자에 사용자 정의 xml을 만들었습니다. xml에 admob을 추가하면 실행되지 않습니다. 그래서 프로그래밍 방식으로 시도했습니다. 그러나 여전히 그것을 작동하게 만들 수는 없습니다.

public void OnClickButton(View paramView) 
    { 
    int btn_id = paramView.getId(); 

     if (btn_id == R.id.hint_field) 
     { 
    //set up dialog 
      if (hint != null && hint.length()>0) { 
      final Dialog dialog = new Dialog(Activity.this); 
      dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON); 

      //ad loading 
      dialog.setContentView(R.layout.custom_dialog); 
      RelativeLayout layout = (RelativeLayout)findViewById(R.id.dialog_l); 
      layout.addView(ad); 
      AdRequest r = new AdRequest(); 
      ad.loadAd(r); 

      dialog.setTitle("Σχετικά με την λέξη :"); 
      dialog.setCancelable(true); 

      //set up text 
      TextView text = (TextView) dialog.findViewById(R.id.hint_text); 
      text.setText(hint); 



     //set up button 
      Button button = (Button) dialog.findViewById(R.id.Button01); 
      button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
        dialog.dismiss(); 
       } 
      }); 

      // now that the dialog is set up, it's time to show it  
      dialog.show(); 
      dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher); 
    } 
    } 

버튼을 클릭하면 맞춤 대화 상자가 표시됩니다. 내 활동의 한 OnCreate 방법 나는 같이 adView

AdView ad = new AdView(this, AdSize.BANNER, "a15xxxxxxxxxxx"); 

난에서 NullPointerException이 얻을 만든 : layout.addView (광고를)

아이디어가 있으십니까?

미리 감사드립니다.

+0

이 광고 객체가 – jcw

+0

를 생성 코드의 일부를 게시 할 수 당신은 반드시'custom_dialog.xml'은'안드로이드와 RelativeLayout의가 : ID = "dialog_l"'를? –

답변

5

문제가 해결되었습니다. 먼저 사용자 정의 대화 상자 인터페이스를 확장해야했습니다. 아래 코드는 작업 코드입니다!

public void OnClickButton(View paramView) 
    { 
    int btn_id = paramView.getId(); 

     if (btn_id == R.id.hint_field) 
     { 
    //set up dialog 
      if (hint != null && hint.length()>0) { 
      final Dialog dialog = new Dialog(Activity.this, R.style.Theme_New); 
      dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON); 
      dialog.setContentView(R.layout.custom_dialog); 

      LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View main = inflater.inflate(R.layout.custom_dialog, null); 
      dialog.setContentView(main); 
      WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 

      LinearLayout linear = (LinearLayout)main.findViewById(R.id.ad_layout); 

       ad = new AdView(this, AdSize.IAB_MRECT, "a15xxxxxxxx"); 


     AdRequest request = new AdRequest(); 
     Set<String> keywords = new HashSet<String>(); 
     keywords.add("game"); 
     request.setKeywords(keywords); 

      linear.addView(ad); 
      ad.loadAd(request); 

      dialog.setTitle("Title :"); 
      dialog.setCancelable(true); 

      //set up text 
      TextView text = (TextView) dialog.findViewById(R.id.hint_text); 
      text.setText(hint); 


     //set up button 
      Button button = (Button) dialog.findViewById(R.id.Button01); 
      button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
        dialog.dismiss(); 
       } 
      }); 

      // now that the dialog is set up, it's time to show it  
      lp.width = width; 
      lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
      dialog.show(); 
      dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher); 
      dialog.getWindow().setAttributes(lp); 
      dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT; 
      dialog.getWindow().getAttributes().height = LayoutParams.WRAP_CONTENT; 
    } 
    }  
} 
+0

굉장한 답변 주셔서 감사합니다 !! – Coderji