【问题标题】:android NoClassDefFound errorandroid NoClassDefFounderror
【发布时间】:2014-11-17 03:54:55
【问题描述】:

我是 android 编码的新手,今天遇到了一个问题。几个小时前我可以运行它,但突然它开始给我一个 NoClassDefFound 错误。请帮我找出为什么它突然开始给我错误。

Logcat:

11-16 22:44:34.546: E/AndroidRuntime(31850): FATAL EXCEPTION: main
11-16 22:44:34.546: E/AndroidRuntime(31850): Process: com.Blocks.blocks, PID: 31850
11-16 22:44:34.546: E/AndroidRuntime(31850): java.lang.NoClassDefFoundError: com.Blocks.blocks.Play
11-16 22:44:34.546: E/AndroidRuntime(31850):    at com.Blocks.blocks.MainActivity$1.onClick(MainActivity.java:35)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at android.view.View.performClick(View.java:4445)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at android.view.View$PerformClick.run(View.java:18446)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at android.os.Handler.handleCallback(Handler.java:733)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at android.os.Looper.loop(Looper.java:136)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at android.app.ActivityThread.main(ActivityThread.java:5146)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at java.lang.reflect.Method.invoke(Method.java:515)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
11-16 22:44:34.546: E/AndroidRuntime(31850):    at dalvik.system.NativeStart.main(Native Method)

这是我的 MainActivity.java(包括所有导入)

public class MainActivity extends Activity {

    Button play,Exits,AboutMe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AdView adView = (AdView) this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("12A7F007F53439A00E30C06216544A0B").build();
        adView.loadAd(adRequest);
        play=(Button)findViewById(R.id.Play);
        Exits=(Button)findViewById(R.id.Exit);
        AboutMe=(Button)findViewById(R.id.AboutMe);
        final Context context = this;

        play.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intents=new Intent(context,Play.class);
                startActivity(intents);
            }
        });
        Exits.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
                System.exit(0);
            }
        });
        AboutMe.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent(context,AboutMe.class);
                startActivity(i);
            }
        });
    }

第 35 行是这样的:

Intent intents=new Intent(MainActivity.this,Play.class);

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Blocks.blocks"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="21"
    />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
        <activity
            android:screenOrientation="portrait"
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:screenOrientation="portrait"
            android:name=".AboutMe"
            android:label="@string/AboutT" >
        </activity>
        <activity
            android:screenOrientation="landscape"
            android:name=".Play"
            android:theme="@android:style/Theme.NoTitleBar"
            android:label="@string/Play" >
        </activity>
        <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    </application>

</manifest>

【问题讨论】:

  • 我和你有同样的问题,我看到你和我有同样的模式。原因似乎是 screenOrientation,我知道是因为我在将 screenOrientation 强制为横向之后遇到了 NoClassDefFound 错误并且我还没有找到任何解决方案..

标签: java android noclassdeffounderror


【解决方案1】:

尝试正常工作后清理项目。

【讨论】:

  • 你的意思是干净的项目?如果您的意思是干净的项目,我已经这样做了,但它不起作用。
  • 我在我的 android studio 项目中遇到了同样的问题。在我身边工作良好后清理项目。
  • 请参考这个链接非常有用stackoverflow.com/questions/9833607/…
【解决方案2】:

你可以使用:

Intent i=new Intent(MainActivity.this,AboutMe.class);

而不是

Intent i=new Intent(context,AboutMe.class);

【讨论】:

  • 感谢您的快速回复,但它给了我同样的错误):
【解决方案3】:

活动上下文仅在活动方法中可用,因此不要在活动类中设置上下文,而是在onCreate方法中将其设置为

context = this;

并在您的活动中声明为Context context; 而不是final Context context = this; 即将你的代码重写为

 Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;  // add here

或改变

Intent intents=new Intent(context,Play.class);

Intent intents=new Intent(MainActivity.this,Play.class);

【讨论】:

  • 我已经完成了这两项,第一个迫使我将其更改为最终版本,否则上下文不起作用。第二个给了我和以前一样的错误代码。谢谢你这么快回复。
  • @AndrewChan 然后将Context context; 更改为final Context context; 并清除您的项目并重试。同时发布清单文件。
  • 我也这样做了哈哈。崩溃并再次给我错误。
【解决方案4】:

Play 是上面代码中Button 类型的变量。您是否在 Play.class 某处定义了名为Play 的活动?如果是这样,Play 变量会干扰看到它。通常,Java 编码风格表示类名以大写字母开头,变量名以小写字母开头。 Play.class 看起来像静态引用,但实际上只是对 Button 的常规引用。

我的猜测是您提到引用不同的活动。如果您有 Play.class 和 Play Activity,请将 Play 重命名为 play(更改除问题行之外的所有引用)

【讨论】:

  • 你在某处有一个名为 Play.java 的类吗?
  • 是的,我可以发布它。但是我会警告你,它非常丑陋……丑陋的意思是我很懒惰,把我所有的代码都放到了那个文件中哈哈。
【解决方案5】:

我遇到过几次这个问题,它可以通过清理项目很容易地删除,而不是重新安装它,如果你选择不编写 getApplicationContext(); ,Blaze Tama 显示的是编写意图以更改活动的更可取的方式;

【讨论】:

  • 我该如何重新安装项目?是的,我认为这种方式会更好。
  • 好吧 ctrl+F11 做同样的事情,清理项目对你的问题有帮助吗?
  • 您是否对 Play 活动进行了任何您认为可能导致错误的更改?
  • 我不记得了。在我将我的应用程序发布到市场上之后,我没有碰它。当时效果很好。
  • 你在你的应用程序中添加了 AddMob 并且它停止工作了,是这样吗?
【解决方案6】:

改成:

Intent intents=new Intent(MainActivity.this, Play.class);

更新

似乎是因为您有 2 个 Play :一个是 Button 的实例,一个是 Activity。将button 的实例更改为播放(小写 p),此代码应该可以工作。

【讨论】:

  • 我试过了,但它给了我和以前一样的错误信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 2015-01-25
  • 2014-07-01
  • 2013-09-21
相关资源
最近更新 更多