简体   繁体   English

android NoClassDefFound 错误

[英]android NoClassDefFound error

I'm new to coding in android and I ran into a problem today.我是 android 编码的新手,今天遇到了一个问题。 I was able to run this a couple of hours ago but suddenly it just started giving me a NoClassDefFound error.几个小时前我能够运行它,但突然间它开始给我一个 NoClassDefFound 错误。 Please help me find out why it suddenly started giving me errors.请帮我找出为什么它突然开始给我错误。

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)

and here is my MainActivity.java (All imports are included)这是我的 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);
            }
        });
    }

Line 35 is this:第 35 行是这样的:

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

Manifest:显现:

<?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>

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

You can use:您可以使用:

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

instead Of代替

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

Change it to :将其更改为:

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

UPDATE更新

It seems because you have 2 Play : One is an instance of Button and one is Activity .似乎是因为您有 2 个Play :一个是Button的实例,一个是Activity Change the instance of button to play ( lowercase p) and this code should work.button的实例更改为播放(小写p),此代码应该可以工作。

Activity context available in activity methods only,so instead of setting context in activity class set it in onCreate method as活动上下文仅在活动方法中可用,因此不是在活动类中设置上下文,而是在onCreate方法中将其设置为

context = this;

and declare in your activity as Context context;并在您的活动中声明为Context context; instead of final Context context = this;而不是final Context context = this; ie rewrite your code as即重写你的代码

 Context context;

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

or change或改变

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

to

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

Play is a variable of type Button in your code above. Play是上面代码中Button类型的变量。 Do you have an activity named Play defined in Play.class somewhere?你在 Play.class 的某个地方定义了一个名为Play的活动吗? If so, the Play variable is interfering with seeing it.如果是这样,则Play变量会干扰查看它。 Usually, Java coding styles says that class name start with capital letter and variable names start with a lower case letter.通常,Java 编码风格说类名以大写字母开头,变量名以小写字母开头。 Play.class looks like a static reference, but it is really just a regular reference to a Button . Play.class看起来像一个静态引用,但它实际上只是对Button的常规引用。

My guess is you mention to reference a different activity.我的猜测是你提到引用不同的活动。 If you have Play.class with a Play Activity , rename Play to play (change all of references except the problem line)如果您有Play.class和 Play Activity ,将Play重命名为play (更改除问题行之外的所有引用)

我遇到过几次这个问题,它可以通过清理项目而不是重新安装它来轻松删除,如果您选择不编写 getApplicationContext(),那么 Blaze Tama 所展示的是编写意图以更改活动的非常可取的方式;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM