Andrew Chan
Andrew Chan

Reputation: 25

android NoClassDefFound error

I'm new to coding in android and I ran into a problem today. I was able to run this a couple of hours ago but suddenly it just started giving me a NoClassDefFound error. 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)

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:

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>

Upvotes: 1

Views: 167

Answers (6)

Blaze Tama
Blaze Tama

Reputation: 10948

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. Change the instance of button to play (lowercase p) and this code should work.

Upvotes: 0

Thirumoorthy
Thirumoorthy

Reputation: 589

Clean Project after try it working fine.

Upvotes: 1

Pankaj Nimgade
Pankaj Nimgade

Reputation: 4549

I had that issue few times, it can easily be removed by cleaning the project and than reinstalling it, what Blaze Tama showed is quite preferable way of writing intents to change activity if you choose not to write getApplicationContext();

Upvotes: 0

iagreen
iagreen

Reputation: 32026

Play is a variable of type Button in your code above. Do you have an activity named Play defined in Play.class somewhere? If so, the Play variable is interfering with seeing it. Usually, Java coding styles says that class name start with capital letter and variable names start with a lower case letter. Play.class looks like a static reference, but it is really just a regular reference to a 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)

Upvotes: 0

Giru Bhai
Giru Bhai

Reputation: 14408

Activity context available in activity methods only,so instead of setting context in activity class set it in onCreate method as

context = this;

and declare in your activity as Context context; instead of final Context context = this; i.e. 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);

Upvotes: 0

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

You can use:

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

instead Of

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

Upvotes: 0

Related Questions