MvvmCross / MvvmCross-AndroidSupport

Android support library packages for MvvmCross: The .NET MVVM framework for cross-platform solutions.
http://mvvmcross.com
15 stars 0 forks source link

Unable to find explicit activity class {packagename.schoolapp/schoolapp.droid.FragmentOne}; have you declared this activity in your AndroidManifest.xml? #287

Closed ahadk closed 8 years ago

ahadk commented 8 years ago

FragmentOne.cs

using Android.OS;
using Android.Runtime;
using Android.Views;
using SchoolApp.Core.ViewModels;
using MvvmCross.Droid.Shared.Attributes;
using MvvmCross.Droid.Support.V4;

namespace SchoolApp.Droid
{

[MvxFragmentAttribute(typeof(MainViewModel), Resource.Id.frameLayout)]
[Register("schoolapp.droid.FragmentOne")]
public class FragmentOne : MvxFragment<FragmentOneViewModel>
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.Inflate(Resource.Layout.MyListView, container, false);
    }
}
}

FragmentOneViewModel.cs

using MvvmCross.Core.ViewModels;

namespace SchoolApp.Core.ViewModels
{
public class FragmentOneViewModel : MvxViewModel
{
}
}`

MainActivity.cs
`using System.Linq;
using Android.App;
using Android.OS;
using Android.Support.V4.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
using SchoolApp.Core.ViewModels;
using MvvmCross.Droid.Support.V7.AppCompat;
using Fragment = Android.Support.V4.App.Fragment;
using Toolbar = Android.Support.V7.Widget.Toolbar;

namespace SchoolApp.Droid
{

[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : MvxCachingFragmentCompatActivity<MainViewModel>
{
    ActionBarDrawerToggle _drawerToggle;

    ListView _drawerListView;

    DrawerLayout _drawerLayout;

    Fragment[] _fragments = { new FragmentOne(), new FragmentTwo() };
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.DrawerScreen);

        var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);

        SupportActionBar.SetDisplayHomeAsUpEnabled(true);

        _drawerListView = FindViewById<ListView>(Resource.Id.drawerListView);
        _drawerListView.ItemClick += (s, e) => ShowFragmentAt(e.Position);
        _drawerListView.Adapter = new ArrayAdapter<string>(this, global::Android.Resource.Layout.SimpleListItem1, ViewModel.MenuItems.ToArray());

        _drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayout);

        _drawerToggle = new ActionBarDrawerToggle(this, _drawerLayout, Resource.String.OpenDrawerString, Resource.String.CloseDrawerString);

        _drawerLayout.SetDrawerListener(_drawerToggle);

        ShowFragmentAt(0);
    }

    void ShowFragmentAt(int position)
    {
        //SupportFragmentManager
        //.BeginTransaction()
        //.Replace(Resource.Id.frameLayout, _fragments[position])
        //.Commit();

        ViewModel.NavigateTo(position);

        Title = ViewModel.MenuItems.ElementAt(position);

        _drawerLayout.CloseDrawer(_drawerListView);
    }

    protected override void OnPostCreate(Bundle savedInstanceState)
    {
        _drawerToggle.SyncState();

        base.OnPostCreate(savedInstanceState);
    }

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        if (_drawerToggle.OnOptionsItemSelected(item))
            return true;

        return base.OnOptionsItemSelected(item);
    }
}
}

MainViewModel.cs using MvvmCross.Core.ViewModels;

using System;
using System.Collections.Generic;

namespace SchoolApp.Core.ViewModels
{
public class MainViewModel : MvxViewModel
{
readonly Type[] _menuItemTypes1 = {
typeof(FragmentOneViewModel),
typeof(FragmentTwoViewModel),
};

    public IEnumerable<string> MenuItems { get; private set; } = new[] { "My List", "My Settings" };

    public void ShowDefaultMenuItem()
    {
        NavigateTo(0);
    }

    public void NavigateTo(int position)
    {
            ShowViewModel(_menuItemTypes1[position]);
    }
}

public class DrawerItem : Tuple<string, Type>
{
    public DrawerItem(string displayName, Type viewModelType)
        : base(displayName, viewModelType)
    { }

    public string DisplayName
    {
        get { return Item1; }
    }

    public Type ViewModelType
    {
        get { return Item2; }
    }
}
}

plugin used : mvvmCross.Droid.shared (not using droid.fragging) Droid.support.V4 droid.support.v7.AppCompat

Having Error: Unable to find explicit activity class {com.grappetite.schoolapp/schoolapp.droid.FragmentOne}; have you declared this activity in your AndroidManifest.xml?

Things i done : removing / adding plugins try checking manifest.xml (not useful to edit since it will override on runtime)

Possible guess : somethings seems wrong with register annotation, can anyone point out what going wrong? Thanks :)

Cheesebaron commented 8 years ago

I've gone ahead and edited your issue. Use tripple ` when writing code blocks, or just indent your code...

Also please add some context to your issue. It does not make entirely sense what you are trying to convey. I.e. fill out the Issue template that you removed when opening a new issue.

ahadk commented 8 years ago

Thanks Tomasz.

kjeremy commented 8 years ago

FragmentOne is a Fragment not an Activity. Are you using the MvxFragmentsPresenter in your Setup::CreateViewPresenter()? Alternatively make sure that Setup.cs inherits from MvxAppCompatSetup instead of MvxAndroidSetup which should set that up for you.

ahadk commented 8 years ago

Prefect Thanks ! ViewPresenter was missing in Setup.cs file.

ahadk commented 8 years ago

If someone struggling with the same issue, here is the ViewPresenter.

protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
    var mvxFragmentsPresenter = new MvxFragmentsPresenter(AndroidViewAssemblies);
    Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxFragmentsPresenter);
    return mvxFragmentsPresenter;
}
Cheesebaron commented 8 years ago

I've edited your post (again!) to show your code block properly. TRIPPLE `!