2014-02-19 18 views
0

錯誤:致命異常:主要誤差Android開發

02-19 21:28:52.920: E/AndroidRuntime(17041): FATAL EXCEPTION: main 
02-19 21:28:52.920: E/AndroidRuntime(17041): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gamecik/com.example.gamecik.MainActivity}: java.lang.ClassCastException: com.google.android.gms.ads.AdView cannot be cast to android.widget.LinearLayout 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.access$700(ActivityThread.java:165) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.os.Looper.loop(Looper.java:137) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.main(ActivityThread.java:5450) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at java.lang.reflect.Method.invoke(Method.java:525) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at dalvik.system.NativeStart.main(Native Method) 
02-19 21:28:52.920: E/AndroidRuntime(17041): Caused by: java.lang.ClassCastException: com.google.android.gms.ads.AdView cannot be cast to android.widget.LinearLayout 
02-19 21:28:52.920: E/AndroidRuntime(17041): at com.example.gamecik.MainActivity.onCreate(MainActivity.java:32) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.Activity.performCreate(Activity.java:5369) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267) 

當我使用AdMob的代碼我的應用程序是error..if我中刪除AdMob廣告代碼我的應用是很好的.. 請幫我...對不起 這個問題沒有給出太多的信息,我只是不知道要給你什麼信息(如果這是有道理的話)?請回復並告訴我你需要什麼信息,並感謝你的任何和所有幫助

這是我的代碼:

我的活動JAVA

package com.example.gamecik; 
import com.google.android.gms.ads.AdSize; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 

import android.os.Bundle; 
import android.app.Activity; 
import android.media.MediaPlayer; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.LinearLayout; 


public class MainActivity extends Activity implements OnClickListener { 

     private MediaPlayer mp; 
     private AdView adView; 
     private static final String AD_UNIT_ID = "ca-app-pub-1715534499344773/3123443647"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     adView = new AdView(this); 
     adView.setAdUnitId(AD_UNIT_ID); 
     adView.setAdSize(AdSize.BANNER); 

     LinearLayout layout = (LinearLayout)findViewById(R.id.adView); 

     layout.addView(adView); 

     AdRequest adRequest = new AdRequest.Builder().build(); 
     adView.loadAd(adRequest); 


     // Add click listeners to all the ImageButtons 

     View button1 = findViewById(R.id.sescalbuton); 
     View button2 = findViewById(R.id.magnumsilah); 
     View button3 = findViewById(R.id.Button03); 
     View button4 = findViewById(R.id.Button02); 
     button1.setOnClickListener(this); 
     button2.setOnClickListener(this); 
     button3.setOnClickListener(this); 
     button4.setOnClickListener(this); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    // Required method if OnClickListener is implemented 

    @Override 
    public void onClick(View v) { 

     // Find which ImageButton was pressed and take appropriate action 

     switch(v.getId()){ 

     // The cow button 
     case R.id.sescalbuton: 
      mp = MediaPlayer.create(this, R.raw.ak47); 
     break; 

     // The duck button 
     case R.id.magnumsilah: 
      mp = MediaPlayer.create(this, R.raw.magnum); 
     break; 

     // The sheep button     
     case R.id.Button03: 
      mp = MediaPlayer.create(this, R.raw.shotgun); 
     break; 

     // The uzi    
     case R.id.Button02: 
      mp = MediaPlayer.create(this, R.raw.uzi); 
     break; 

     } 

     mp.seekTo(0); 
     mp.start(); 
    } 
} 

MY XML代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/wood" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/sescalbuton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/shotgun" 
     android:layout_below="@+id/magnumsilah" 
     android:background="@drawable/ak47" /> 

    <Button 
     android:id="@+id/Button03" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/sescalbuton" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/sohtgun" /> 

    <Button 
     android:id="@+id/Button02" 
     android:layout_width="150dp" 
     android:layout_height="110dp" 
     android:layout_above="@+id/sescalbuton" 
     android:layout_alignRight="@+id/sescalbuton" 
     android:background="@drawable/guns" /> 

    <com.google.android.gms.ads.AdView 
     xmlns:ads="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/adView" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         ads:adUnitId="ca-app-pub-1715534499344773/3123443647" 
         ads:adSize="BANNER"/> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/adView" 
     android:layout_alignTop="@+id/adView" 
     android:layout_marginTop="28dp" 
     android:src="@drawable/supperweapon" /> 

    <Button 
     android:id="@+id/magnumsilah" 
     android:layout_width="150dp" 
     android:layout_height="100dp" 
     android:layout_alignLeft="@+id/imageView1" 
     android:layout_below="@+id/imageView1" 
     android:background="@drawable/magnum" /> 

</RelativeLayout> 

請幫我...什麼是問題呢?

+1

錯誤很明顯:從第二行開始:「AdView不能轉換爲android.widget.LinearLayout」,稍微向下(第15行):「由於:java.lang.ClassCastException:com.google。 android.gms.ads.AdView無法在com.example.gamecik.MainActivity.onCreate(MainActivity.java:32)中轉換爲android.widget.LinearLayout「。因此,請查看源代碼中的第32行,將「AdView」作爲「LinearLayout」進行類型轉換。你不能這麼做,因爲錯誤信息清楚地表明。閱讀錯誤信息中的實際單詞 - 它們通常實際上有意義。 –

+0

請發佈您的XML – nikis

回答

2

如果我理解正確,鑄造不是你唯一的問題。您正試圖將AdView添加到您的活動XML中不存在的LinearLayout。但是,在您的XML中,您已經添加了AdView並且動態地啓動了AdView,這沒有任何意義。將您的onCreate(..)代碼更改爲以下代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    adView = (AdView) findViewById(R.id.adView); 
    adView.setAdUnitId(AD_UNIT_ID); 
    adView.setAdSize(AdSize.BANNER); 

    AdRequest adRequest = new AdRequest.Builder().build(); 
    adView.loadAd(adRequest); 

    //.... rest of your onCreate() code 
} 
1

尋找你的佈局文件中的任何R.id.adview。這不是一個LinearLayout,但你試圖把它作爲一個。這就是它崩潰的原因。可能是一個AdView?

1

這個問題似乎在這裏,

LinearLayout layout = (LinearLayout)findViewById(R.id.adView); 

你在哪裏我相信想投你的AdView佈局,線性佈局。

將R.id.adView更改爲您的LinearLayout的ID。如果這不是他的情況,也要發佈你的佈局xml。

0

嘗試在更改位置後清理項目。這對我有用!