Question

I've just created a sliding menu but when I press the items from the menu they are not opening the activities I've set. Here's my code for onItemClick method:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // Highlight the selected item, update the title, and close the drawer
    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);         
    //You should reset item counter 
    mDrawer.closeDrawer(mDrawerList);
    if ( position == 1 ) {
        Intent intent = new Intent(MainActivity.this, PrimActivitate.class);
        startActivity(intent);
        mDrawer.closeDrawers();
        if ( position == 2 ) {
            Intent intent2 = new Intent(MainActivity.this, Profesori.class);
            startActivity(intent2);
            mDrawer.closeDrawers();
        }

The problem is: when I press the first item, it's crashing the app and when I press the second it's not doing anything..

EDIT:

02-20 15:27:43.991: E/AndroidRuntime(1278): FATAL EXCEPTION: main
02-20 15:27:43.991: E/AndroidRuntime(1278): Process: com.orar.cngcnasaud, PID: 1278
02-20 15:27:43.991: E/AndroidRuntime(1278): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.orar.cngcnasaud/com.orar.cngcnasaud.PrimActivitate}; have you declared this activity in your AndroidManifest.xml?
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.app.Activity.startActivityForResult(Activity.java:3424)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.app.Activity.startActivityForResult(Activity.java:3385)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.app.Activity.startActivity(Activity.java:3627)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.app.Activity.startActivity(Activity.java:3595)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at com.orar.cngcnasaud.MainActivity$DrawerItemClickListener.onItemClick(MainActivity.java:179)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.os.Handler.handleCallback(Handler.java:733)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.os.Looper.loop(Looper.java:136)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at java.lang.reflect.Method.invoke(Method.java:515)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-20 15:27:43.991: E/AndroidRuntime(1278):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

That never go to position == 2 as i see, close if ( position == 1 ) before if ( position == 2 ), try that:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    mDrawerList.setItemChecked(position, true);         
    mDrawer.closeDrawer(mDrawerList);
    if ( position == 1 ) {
        Intent intent = new Intent(MainActivity.this, PrimActivitate.class);
        MainActivity.this.startActivity(intent);
        mDrawer.closeDrawers();
        }
    if ( position == 2 ) {
        Intent intent2 = new Intent(MainActivity.this, Profesori.class);
        MainActivity.this.startActivity(intent2);
        mDrawer.closeDrawers();
        }

}

About the crash you forgot to add to your AndroidManifest.xml:

<activity android:label="@string/app_name" android:name="com.orar.cngcnasaud.PrimActivitate"/>
<activity android:label="@string/app_name" android:name="com.orar.cngcnasaud.Profesori"/>

OTHER TIPS

When you press second item onItemClick() was doing nothing, since if ( position == 2) is inside if ( position == 1).

Can you post crash stack trace as well?

As your logcat suggest, you have not declared your PrimActivitate in the manifest. Add these to your AndroidManifest:

<activity android:name=".PrimActivitate"></activity>

Add the second as well if not there:

<activity android:name=".Profesori"></activity>

Next, move your second condition OUTSIDE first, so that both your cases can be handled:

if (position == 1) {
    Intent intent = new Intent(MainActivity.this, PrimActivitate.class);
    startActivity(intent);
    mDrawer.closeDrawers();
}
else if (position == 2) {
    Intent intent2 = new Intent(MainActivity.this, Profesori.class);
    startActivity(intent2);
    mDrawer.closeDrawers();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top