Java 致命异常:Android 开发的主要错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21890899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:17:25  来源:igfitidea点击:

FATAL EXCEPTION: main error Android development

javaandroidexception

提问by user3325784

ERROR :

错误 :

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.access0(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)

When I use Admob code my application is error..if I delete Admob code my application is good.. PLEASE HELP ME... Sorry that this question hasn't given much information, I just don't know what information to give to you (if that makes sense?) Please respond and tell me what information you would need and thank you for any and all of your help

当我使用 Admob 代码时,我的应用程序出错..如果我删除 Admob 代码,我的应用程序很好.. 请帮助我... 抱歉,这个问题没有提供太多信息,我只是不知道该提供什么信息你(如果这是有道理的?)请回复并告诉我你需要什么信息,并感谢你的任何和所有帮助

HERE IS MY CODE:

这是我的代码:

MY ACTIVITY JAVA

我的活动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 CODE:

我的 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>

PLEASE HELP ME...WHAT IS PROBLEM ?

请帮助我......什么问题?

采纳答案by Aashir

If I understood correctly, casting is not your only issue. You're trying to add AdView to a LinearLayout that doesn't exist in your activities XML. But instead in your XML you've added an AdView and your initiating an AdView dynamically as well, which doesn't make any sense. Change your onCreate(..) code to the following:

如果我理解正确,铸造不是你唯一的问题。您正在尝试将 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
}

回答by Ben Kane

Look for whatever R.id.adview is in your layout file. It's not a LinearLayout but you're trying to cast it as one. That's why it's crashing. Probably is an AdView?

在您的布局文件中查找任何 R.id.adview。它不是 LinearLayout,但您正试图将其作为一个进行投射。这就是它崩溃的原因。可能是 AdView?

回答by Atul O Holic

The problem seems to be here,

问题好像出在这里

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

where you are I believe trying to cast your AdView layout to a linear layout.

我相信您在哪里尝试将您的 AdView 布局转换为线性布局。

Change R.id.adView to the id of your LinearLayout. Post your layout xml too if this is not he case.

将 R.id.adView 更改为您的 LinearLayout 的 id。如果不是这种情况,也可以发布您的布局 xml。

回答by Dimitri Dewaele

Try to clean the project after changing the place. This worked for me!

换地方后尝试清理项目。这对我有用!