1

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>
  • I have the same problem with you and I see the same pattern that you and I have. The cause seems to be screenOrientation, I know because I hit on the NoClassDefFound error after I force my screenOrientation to landscape and I haven’t found any solution yet.. – Jeff Feb 04 '20 at 16:55

6 Answers6

1

Clean Project after try it working fine.

Thirumoorthy
  • 589
  • 2
  • 11
0

You can use:

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

instead Of

Intent i=new Intent(context,AboutMe.class);
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
0

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.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
0

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);
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • I've done both of those, the first one forces me to change it to final or else context does not work. The second one gives me same error code as before. Thanks for replying so fast though. – Andrew Chan Nov 17 '14 at 04:08
  • @AndrewChan then change `Context context;` to `final Context context;` and clear your project and retry.And also post manifest file. – Giru Bhai Nov 17 '14 at 04:10
  • I've done that as well haha. Crashes and gives me the error again. – Andrew Chan Nov 17 '14 at 04:13
0

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)

iagreen
  • 31,470
  • 8
  • 76
  • 90
0

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();

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30