我正在构建一个Android应用程序,它使用在此处找到的自定义构建的TwoDScrollView:
http://blog.gorges.us/2010/06/android-two-dimensional-scrollview/
可以在其他几个网站上找到同一个类,Stack Overflow上的其他人也提出了有关它的问题.我在以前使用Java/Eclipse构建的Android应用程序中使用它,并且我取得了成功.
在我目前的应用程序中,我想使用C#和MonoDroid.我决定用C#重写整个TwoDScrollView类.在重写它,然后在一些布局XML中使用它之后,我在尝试运行我的代码时遇到以下异常:
抛出了System.NotSupportedException.无法从本机句柄44f4d310激活MyProject.TwoDScrollView类型的实例.
System.Exception:找不到MyProject.TwoDScrollView ::.ctor(System.IntPtr,Android.Runtime.JniHandleOwnership)......的构造函数......后面有更多文本....
我的布局XML如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<myproject.TwoDScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</myproject.TwoDScrollView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
根据以下链接中有关在MonoDroid中使用布局XML中的自定义视图的说明:http://docs.xamarin.com/android/advanced_topics/using_custom_views_in_a_layout
TwoDScrollView类的构造函数如下所示:
public TwoDScrollView(Context context)
: base(context)
{
initTwoDScrollView();
}
public TwoDScrollView(Context context, IAttributeSet attrs)
: base(context, attrs)
{
initTwoDScrollView();
}
public TwoDScrollView(Context context, IAttributeSet attrs, int defStyle)
: base(context, attrs, defStyle)
{
initTwoDScrollView();
}
Run Code Online (Sandbox Code Playgroud)
C#版本中存在与Java版本相同的构造函数(可以在上面的链接中找到).什么可能出错?如果有人想看,我可以发布我的TwoDScrollView的完整C#代码.除了用C#重写之外,它基本上与位的Java代码位相同.
谢谢你的帮助!
android scrollview android-custom-view android-layout xamarin.android
在测试包含几个Entry文本框和启用了地理定位的地图的Android应用程序时,我发现如果在屏幕键盘启动时单击Android上的后退按钮(即,在其中一个条目上插入文本时) ),应用程序将崩溃,我将在调试输出上记录以下异常:
09-09 00:10:38.187 I/MonoDroid(14174): UNHANDLED EXCEPTION: System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Maps.Android.MapRenderer from native handle b250001d ---> System.MissingMethodException: No constructor found for Xamarin.Forms.Maps.Android.MapRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
整个堆栈跟踪在这里:http://pastebin.com/hDAS4JLV
要注意的一些事项:
1 - 这些文本框上使用的键盘类型是文本和电话.
2 - 与堆栈跟踪所说的相反,我根本不在应用程序上使用相机.
3 - MapRenderer在Xamarin Forms上没有这样的构造函数.请注意,我正在使用Xamarin.Forms 1.2.3(-Pre版本).
4 - 我尝试添加一个自定义地图渲染器和一个虚拟构造函数,根据异常接受所需的参数,但无济于事.
5 - 只有在显示屏幕键盘时,才会在任何其他(已测试)情况下发生这种情况.
6 - 在iOS版本中不会发生这种情况.
7 - 在几个设备(Galaxy Nexus,Galaxy S5,Alcatel One Touch)上测试相同的步骤,结果相同.
我知道Xamarin Android中的构造函数难度,如下所述: 没有找到构造函数...(System.IntPtr,Android.Runtime.JniHandleOwnership)
以及我在应用程序中创建的所有片段和活动以及其他自定义视图都会导入此构造函数.
但有时null reference exception会抛出一个OnCreateView方法.例:
public class TestView: Android.Support.V4.App.Fragment{
public TestClass _testClass;
public TestView (TestClass testClass)
{
this._testClass = testClass;
}
public TestView(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override Android.Views.View OnCreateView (Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
{
View view = inflater.Inflate (Resource.Layout.Fragment_TestView, container, false);
_testClass.ExecuteTestMethod();
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
这一段代码抛出一个null reference exception在OnCreateView,occassionaly.这很少发生,从来没有直接从代码创建视图然后测试它.
现在很明显,此代码中唯一可以抛出异常的点是_testClass变量.所以现在显然我的问题是,在调用构造函数时是否OncreateView也执行了该方法javaReference?