1

I have built a navigation drawer. Once the app launches I direct the user from "MainActivity" to MainMenuActivity through an intent through the following code inside "MainActivity":

    Intent Activity = new Intent(MainActivity.this, MainMenuActivity.class);
    startActivity(Activity);

but when the app launches, it crashes directly with a logcat of:

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {package.allineed/Package.MainMenuActivity}; have you declared this activity in your AndroidManifest.xml?

I declared MainMenuActivity inside AndroidManifest.xml by adding to the application the following code:

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

but the app crashed and gave me the following logcat:

    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{..allineed/...allineed.MainMenuActivity}: java.lang.ClassCastException: ...allineed.MainMenuActivity cannot be cast to android.app.Activity

This is is MainMenuActivity:

public class MainMenuActivity extends Fragment{
View myView;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
     myView=inflater.inflate(R.layout.activity_main_menu,container,false);

    return myView;
}
}

I consumed a lot of time on it, ho possibly could it be solved?

Roberts
  • 101
  • 2
  • 9

3 Answers3

0

Try this -

public class MainMenuActivity extends FragmentActivity

you can take a look at this for more information.

You have to embed the fragment inside an existing activity, you can never directly call startActivity() on a fragment class

Community
  • 1
  • 1
DarshanGowda0
  • 452
  • 3
  • 11
0

First of all MainMenuActivity is a Fragment not an Activity. So shouldn't declare in Manifest.xml as an Activity.

And you can not directly call Fragment using Intent.

By using FragmentManager and FragmentTransaction you can load fragment into the activity.

Just check this link on How to use fragment in activity?

http://android-er.blogspot.in/2011/12/programmatically-add-fragment.html

Jaymin Panchal
  • 2,797
  • 2
  • 27
  • 31
0

if you want a NavigationDrawer, then you main_activity.xml should see as folowed

<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 -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <FrameLayout android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start" />

</android.support.v4.widget.DrawerLayout>

then you can set your navigation in MainActivity via

Fragment fragment = new MainMenuActivity();

// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
     .replace(R.id.left_drawer, fragment)
     .commit();

FrameLayout is a holder, that you can fill with any layout in runtime. The last item in DrawerLayout is ever used for navigation. with android:layout_gravity you can say, where you want the navigation.

anatoli
  • 1,663
  • 1
  • 17
  • 43