我在启动应用时收到此错误消息:按钮无法转换为com.google.android.gms.ads.adView
我的主要xml是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:background="@drawable/fondo_tablero_claro">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ad"
android:layout_marginBottom="10dp"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal">
<Button android:textSize="@dimen/lletra_normal"
android:id="@+id/button_jugar"
android:layout_width="@dimen/botons_width"
android:layout_height="@dimen/botons_hight"
android:layout_marginTop="@dimen/menu_principal_margin_top"
android:text="@string/Jugar" android:textStyle="bold"
android:onClick="callJoc"
android:background="@anim/custom_button">
</Button>
<Button android:textSize="@dimen/lletra_normal"
android:id="@+id/button_millor_puntuacio"
android:layout_width="@dimen/botons_width"
android:layout_height="@dimen/botons_hight"
android:layout_marginTop="@dimen/menu_principal_margin_top"
android:text="@string/Records" android:gravity="center"
android:textStyle="bold" android:onClick="callRecords"
android:background="@anim/custom_button">
</Button>
<Button android:textSize="@dimen/lletra_normal"
android:id="@+id/button_instruccions"
android:layout_width="@dimen/botons_width"
android:layout_height="@dimen/botons_hight"
android:layout_marginTop="@dimen/menu_principal_margin_top"
android:text="@string/Instruccions" android:gravity="center"
android:textStyle="bold" android:onClick="callInstruccions"
android:background="@anim/custom_button">
</Button>
<Button android:textSize="@dimen/lletra_normal"
android:id="@+id/button_shop"
android:layout_width="@dimen/botons_width"
android:layout_height="@dimen/botons_hight"
android:layout_marginTop="@dimen/menu_principal_margin_top"
android:text="@string/shop" android:gravity="center"
android:textStyle="bold" android:onClick="callTienda"
android:background="@anim/custom_button">
</Button>
</LinearLayout>
</ScrollView>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="ca-app-pub-9865093044546350/6563553024"
ads:adSize="SMART_BANNER"/>
</RelativeLayout>
我的.java是:
public class matriX extends Activity
{
Dialog porNiveles, principal;
Intent intent;
Bundle extra;
NumberPicker level;
int NivelMax;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences prefs = this.getSharedPreferences("Scores"
, Context.MODE_PRIVATE);
if (!prefs.contains("score1")){
prefs.edit().putInt("score1",00000).apply();
prefs.edit().putInt("score2",00000).apply();
prefs.edit().putInt("score3",00000).apply();
prefs.edit().putInt("score4",00000).apply();
prefs.edit().putInt("score5",00000).apply();
}
NivelMax = prefs.getInt("nivel", 1);
AdView adView = (AdView)this.findViewById(R.id.ad);
AdRequest adRequest = new AdRequest.Builder().addTestDevice
("****).build();
adView.loadAd(adRequest);
}
public void callJoc(View view){
starts of the dialog
//INIT PRINCIPAL
LinearLayout Candado = (LinearLayout) principal.findViewById(R.id.modoCandado);
LinearLayout Libre = (LinearLayout) principal.findViewById(R.id.modoLibre);
Button Niveles = (Button) principal.findViewById(R.id.botonNivel);
Button cancelar = (Button) principal.findViewById(R.id.bcancel);
cancelar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
principal.cancel();
}
});
Niveles.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
principal.cancel();
porNiveles.show();
}
});
if (NivelMax >= 30) {
Candado.setVisibility(View.GONE);
Libre.setVisibility(View.VISIBLE);
Button but = (Button) principal.findViewById(R.id.botonInfinitoLibre);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
extra.putInt("mode", 0);//modo infinito
intent.putExtras(extra);
startActivity(intent);
}
});
}
else {
Button buto = (Button) principal.findViewById(R.id.botonInfinitoBlock);
buto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Context context = matriX.this;
CharSequence text = getString(R.string.cantAcces);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
});
}
//INIT PORNIVELES
level = (NumberPicker) porNiveles.findViewById(R.id.levelpick);
Button back = (Button) porNiveles.findViewById(R.id.back);
Button empezar = (Button) porNiveles.findViewById(R.id.empezar);
String[] nums_diners = new String[6];
nums_diners[0] = "1";
for(int i = 1; i<nums_diners.length; i++){
nums_diners[i] = Integer.toString((i)*5);
}
level.setMinValue(0);
int valor = NivelMax/5;
if(NivelMax < 26) level.setMaxValue(valor);
else level.setMaxValue(5);
level.setWrapSelectorWheel(false);
level.setDisplayedValues(nums_diners);
empezar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
我的点击监听器 } }); back.setOnClickListener(new OnClickListener(){ @覆盖 public void onClick(查看v){ porNiveles.cancel(); principal.show(); } });
porNiveles.setCanceledOnTouchOutside(true);
principal.setCanceledOnTouchOutside(true);
principal.show();
}
更多函数调用..
答案 0 :(得分:0)
检查您是否使用了ID&#34; ad&#34;在另一个布局中,特别是按钮:
android:id="@+id/ad"
或者将您的AdView ID更改为您确定唯一的另一个:
<com.google.android.gms.ads.AdView
android:id="@+id/ad_foo"
...
AdView adView = (AdView)this.findViewById(R.id.ad_foo);
答案 1 :(得分:0)
似乎我发现了我的错误,我不得不从RelativeLayout中删除这些行:
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
我认为它导致广告和按钮在同一个地方,即使在日食上我也没有看到它