Bug 8508 - Namespace/type name is not getting capitalized with Fragments
Summary: Namespace/type name is not getting capitalized with Fragments
Status: RESOLVED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.2.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2012-11-19 15:15 UTC by Allie Miller
Modified: 2012-11-19 17:44 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Allie Miller 2012-11-19 15:15:24 UTC
Case #20267

There is a namespace/type name is not getting capitalized, and therefore causing the following statement to fail:



fragments.Add(Fragment.Instantiate(this,
typeof(Fragment1).FullName));



It is only looking for Support4.Fragment1, but for some reason will only find it if the first letter is lowercase:

"string typeName = "support4." + typeof(Fragment1).Name;

fragments.Add(Fragment.Instantiate(this, typeName));"



Below is the code that is run:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



using Android.App;

using Android.Content;

using Android.OS;

using Android.Runtime;

using Android.Util;

using Android.Views;

using Android.Widget;

using Android.Support.V4.App;

using Android.Support.V4.View;

using Java.Lang;



namespace Support4

{

[Activity (Label = "@string/fragment_pager_support")]

[IntentFilter (new[]{Intent.ActionMain}, Categories = new[]{
"mono.support4demo.sample" })]

public class FragmentPagerSupport : FragmentActivity

{

const int NUM_ITEMS = 10;

MyAdapter adapter;

ViewPager pager;



protected override void OnCreate (Bundle bundle)

{

base.OnCreate (bundle);



SetContentView(Resource.Layout.fragment_pager);



List<Fragment> fragments = new List<Fragment>();



Log.Info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "type: " +
typeof(FragmentPagerSupport));



//string typeName = "Support4." + typeof
(Fragment1).FullName;

//string typeName = typeof(Fragment1).Name;

string typeName = "support4." + typeof(Fragment1).Name;



//fragments.Add(Fragment.Instantiate(this,
typeof(Fragment1).FullName));

fragments.Add(Fragment.Instantiate(this, typeName));



//Frag



//adapter = new MyAdapter(SupportFragmentManager);



pager = FindViewById<ViewPager>(Resource.Id.pager);

pager.Adapter = adapter;



//pager.Adapter.GetPageWidth();



pager.Post(new Runnable(() => { pager.OffscreenPageLimit =
3; }));

pager.Post(new Runnable(() => {
pager.SetClipChildren(false); }));

//pager.



var button = FindViewById<Button>(Resource.Id.goto_first);

button.Click += (sender, e) => {

pager.CurrentItem = 0; 

};

button =
FindViewById<Button>(Resource.Id.goto_last);

button.Click += (sender, e) => {

pager.CurrentItem = NUM_ITEMS - 1;

};

}



protected class MyAdapter : FragmentPagerAdapter

{

private List<Fragment> _fragments;



public override Java.Lang.Object InstantiateItem(View p0,
int p1)

{

return base.InstantiateItem(p0, p1);

}



public MyAdapter(FragmentManager fm) : base(fm)

{



}



public MyAdapter(FragmentManager fm, List<Fragment>
fragments )

: base(fm)

{

_fragments = fragments;

}



public override int Count {

get {

return NUM_ITEMS;

}

}



/*public override Fragment GetItem (int position)

{

return new ArrayListFragment(position);

}*/



public override Fragment GetItem(int p0)

{

return _fragments[p0];

}



public override float GetPageWidth(int p0)

{

//return base.GetPageWidth(p0);

//base.GetPageWidth(p0);



Log.Info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>",
"--------------------------------------------> GetPageWidth
<-----------------------------------------------");



float ret = 0.5f;

return ret;



//return (float)(0.5f);

}

}



protected class ArrayListFragment : ListFragment

{

int num;



public ArrayListFragment()

{

}



public ArrayListFragment(int num)

{

var args = new Bundle();

args.PutInt("num", num);

Arguments = args;

}



public override void OnCreate (Bundle p0)

{

base.OnCreate (p0);



num = Arguments != null ?
Arguments.GetInt("num") : 1;

}



public override View OnCreateView (LayoutInflater
inflater, ViewGroup container, Bundle savedInstanceState)

{

//container.Layout((container.Right - container.Left)/2,
container.Top, container.Right, container.Bottom);



var v =
inflater.Inflate(Resource.Layout.fragment_pager_list, container, false);

var tv = v.FindViewById<TextView>(Resource.Id.text);

tv.Text = "Fragment #" + num;



return v;

}



public override void OnActivityCreated (Bundle p0)

{

base.OnActivityCreated (p0);



ListAdapter = new
ArrayAdapter<string>(Activity, Android.Resource.Layout.SimpleListItem1,
Cheeses.cheeseStrings);

}



public override void OnListItemClick(ListView l,
View v, int position, long id) {

Console.WriteLine ( "Item clicked: " + id);

}

}

}



public class Fragment1 : Fragment

{

int num;



public Fragment1()

{

}



public Fragment1(int num)

{

var args = new Bundle();

args.PutInt("num", num);

Arguments = args;

}



public override void OnCreate(Bundle p0)

{

base.OnCreate(p0);



num = Arguments != null ? Arguments.GetInt("num") : 1;

}



public View OnCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState)

{

//container.Layout((container.Right - container.Left)/2,
container.Top, container.Right, container.Bottom);



/*var v =
inflater.Inflate(Resource.Layout.fragment_pager_list, container, false);

var tv = v.FindViewById<TextView>(Resource.Id.text);

tv.Text = "Fragment #" + num;



return v;*/



View v =
inflater.Inflate(Resource.Layout.fragment_pager_text_box, container,
false);



return v;

}



public override void OnActivityCreated(Bundle p0)

{

base.OnActivityCreated(p0);



//ListAdapter = new ArrayAdapter<string>(Activity,
Android.Resource.Layout.SimpleListItem1, Cheeses.cheeseStrings);

}



/*public override void OnListItemClick(ListView l, View v, int
position, long id)

{

Console.WriteLine("Item clicked: " + id);

}*/

}

}
Comment 1 Jonathan Pryor 2012-11-19 17:44:08 UTC
> There is a namespace/type name is not getting capitalized, and therefore 
> causing the following statement to fail:
>
>     fragments.Add(Fragment.Instantiate(this, typeof(Fragment1).FullName));

The problem is that the above method cannot work.

Fragment.Instantiate() is a Java method, and the string it accepts is expected to be a Java type name. Consequently, the value you provide needs to be a Java type name, i.e. if you're using C# types it needs to be the Java Android Callable Wrapper for the type:

http://docs.xamarin.com/Android/Guides/Advanced_Topics/Java_Integration_Overview/Android_Callable_Wrappers

Android Callable Wrappers need to follow Java convention (lowercased package name, etc.), and thus can never be PascalCased.

The solution is to use the Java name with your method:

    fragments.Add(Fragment.Instantiate(
            this,
            Java.Lang.Class.FromType (typeof(Activity1)).Name));

Bug #8512 is a feature request to add a Fragment.Instantiate() overload which will take a Type parameter, allowing:

    fragments.Add(Fragment.Instantiate(this, typeof(Activity1)));