Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

FATAL EXCEPTION: main?

Hello.

I keep getting this error every time I press a button in my app:

FATAL EXCEPTION: main
                                                                      Process: com.x.x, PID: 15829
                                                                      java.lang.NoClassDefFoundError: com.x.x.activities.NewItemActivity
                                                                          at com.x.x.activities.MainActivity.startNewItemActivity(MainActivity.java:31)
                                                                          at com.x.x.activities.MainActivity.access$000(MainActivity.java:10)
                                                                          at com.x.x.activities.MainActivity$1.onClick(MainActivity.java:24)
                                                                          at android.view.View.performClick(View.java:4764)
                                                                          at android.view.View$PerformClick.run(View.java:19844)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:135)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5351)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

Even after inspecting the code multiple times, I can't figure out the problem.

Main Activity:

public class MainActivity extends ListActivity {

    private ImageView mAddItemImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAddItemImage = (ImageView) findViewById(R.id.AddItemImage);

        mAddItemImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startNewItemActivity();
            }
        });

    }

    private void startNewItemActivity() {
        Intent intent = new Intent(this, NewItemActivity.class);
        startActivity(intent);
    }
}

Second Activity:

public class NewItemActivity extends AppCompatActivity {

    private ImageView mNewNoteImage;
    private ImageView mNewCategoryImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_item);

        mNewNoteImage = (ImageView) findViewById(R.id.NewNoteImage);
        mNewCategoryImage = (ImageView) findViewById(R.id.NewCategoryImage);
    }
}

Thanks!

1 Answer

aakarshrestha
aakarshrestha
6,509 Points

Change the ListActivity extension on MainActivity to AppCompatActivity. Hope it helps!

Happy coding!

Thanks!