Question

I was following the android navigation drawer guide and all went well until i tried to initiate an activity from the items on the list. The drawer seems to be working fine as the test toast message i created is executed when the first item is clicked however when clicking the second item the app crashes.

Can anyone tell me what the problem is or can anyone please point me in the right direction..

Here's my Mainactivity:

public class MainActivity extends Activity {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mGalaxyTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTitle = mDrawerTitle = getTitle();
    mGalaxyTitles = getResources().getStringArray(R.array.list_items);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);


    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mGalaxyTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    if (savedInstanceState == null) {
        selectItem(0);}

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);


    mDrawerToggle = new ActionBarDrawerToggle(
            this,                 
            mDrawerLayout,         
            R.drawable.ic_drawer,  
            R.string.hello_world,  
            R.string.hello_world  
            ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); 
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); 
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

  /** if (savedInstanceState == null) {
        selectItem(0);
    }**/
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);}


private class DrawerItemClickListener implements ListView.OnItemClickListener {

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

  switch(position) {
    case 0:
        Toast.makeText(getApplicationContext(), "Some Activities MAY take time to load. Please be Patient.", Toast.LENGTH_LONG).show();   
        //Intent a = new Intent(MainActivity.this, Dev_team.class);
            //startActivity(a);
    break;
    case 1:
        Toast.makeText(getApplicationContext(), "Some Activities MAY take time to load. Please be Patient.", Toast.LENGTH_LONG).show(); 
        Intent a = new Intent(getBaseContext(), dev.class);
         startActivity(a);
           break;
    default:
    }
    }
    }


public void selectItem(int position) {
        switch(position) {
    case 0:
        Toast.makeText(getApplicationContext(), "Some Activities MAY take time to load. Please be Patient.", Toast.LENGTH_LONG).show();   
        //Intent a = new Intent(MainActivity.this, Dev_team.class);
            //startActivity(a);
    break;
    case 1:
        setContentView(R.layout.dev_team);
           break;
    default:
        }
    }


@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}



@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    mDrawerToggle.onConfigurationChanged(newConfig);
}


/**public static class GalaxyFragment extends Fragment {
    public static final String ARG_Galaxy_NUMBER = "Galaxy_number";

    public GalaxyFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_Galaxy, container, false);
        int i = getArguments().getInt(ARG_Galaxy_NUMBER);
        String Galaxy = getResources().getStringArray(R.array.list_items)[i];

        int imageId = getResources().getIdentifier(Galaxy.toLowerCase(Locale.getDefault()),
                        "drawable", getActivity().getPackageName());
        ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
        getActivity().setTitle(Galaxy);
        return rootView;
    }**/
}

And here is the activity_main.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/hello_world"
    android:textSize="18sp" />
</RelativeLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

And here is the dev.java

public class dev extends MainActivity{
public void onCreate(Bundle savedInstanceState){
    setContentView(R.layout.dev_team);
}

}

As suggested by Pavel Dudka, Here is the logcat

    06-25 12:05:12.815: E/Trace(778): error opening trace file: No such file or directory (2)
06-25 12:05:13.736: D/dalvikvm(778): GC_FOR_ALLOC freed 100K, 8% free 2668K/2888K, paused 174ms, total 182ms
06-25 12:05:13.746: I/dalvikvm-heap(778): Grow heap (frag case) to 3.792MB for 1127536-byte allocation
06-25 12:05:13.865: D/dalvikvm(778): GC_FOR_ALLOC freed 2K, 6% free 3767K/3992K, paused 120ms, total 120ms
06-25 12:05:13.925: D/dalvikvm(778): GC_CONCURRENT freed 1K, 5% free 3800K/3992K, paused 12ms+12ms, total 54ms
06-25 12:05:14.296: I/Choreographer(778): Skipped 33 frames!  The application may be doing too much work on its main thread.
06-25 12:05:14.366: D/gralloc_goldfish(778): Emulator without GPU emulation detected.
06-25 12:05:24.766: D/AndroidRuntime(778): Shutting down VM
06-25 12:05:24.766: W/dalvikvm(778): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-25 12:05:24.786: E/AndroidRuntime(778): FATAL EXCEPTION: main
06-25 12:05:24.786: E/AndroidRuntime(778): android.content.ActivityNotFoundException: Unable to find explicit activity class    {com.example.navdraw/com.example.navdraw.dev}; have you declared this activity in your AndroidManifest.xml?
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Activity.startActivityForResult(Activity.java:3370)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Activity.startActivityForResult(Activity.java:3331)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Activity.startActivity(Activity.java:3566)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.Activity.startActivity(Activity.java:3534)
06-25 12:05:24.786: E/AndroidRuntime(778):  at com.example.navdraw.MainActivity$DrawerItemClickListener.onItemClick (MainActivity.java:129)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.os.Handler.handleCallback(Handler.java:725)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.os.Looper.loop(Looper.java:137)
06-25 12:05:24.786: E/AndroidRuntime(778):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-25 12:05:24.786: E/AndroidRuntime(778):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 12:05:24.786: E/AndroidRuntime(778):  at java.lang.reflect.Method.invoke(Method.java:511)
06-25 12:05:24.786: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 12:05:24.786: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 12:05:24.786: E/AndroidRuntime(778):  at dalvik.system.NativeStart.main(Native Method)

If there is something Obvious that I am missing please tell me because I am pretty new to app developing community.

Any help is greatly Appreciated.

Thank You!

Was it helpful?

Solution

Well, logcat says you have not declared your activity (the one you are trying to launch) in Manifest file. All activities in Android app must be declared in AndroidManifest.xml file

What you need to do - is to add following section to your manifest (somewhere within application tag):

<application ...>

    <activity
        android:name="com.example.navdraw.dev"
        android:label="label"/>

</application>

More info regarding structure of the manifest file you can find here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top