Xamarin.Android中的NavUtils.NavigateUpFromSameTask

时间:2015-03-12 13:49:05

标签: android xamarin xamarin.android

这是主持人活动:

[Activity(Label = "CrimePagerActivity")]
[MetaData("android.support.PARENT_ACTIVITY", Value = "CriminalIntent.CrimeListActivity")]
public class CrimePagerActivity : FragmentActivity

这是由CrimePagerActivity托管的片段:

public class CrimeFragment : Android.Support.V4.App.Fragment

try
{
    if (NavUtils.ParentActivity != null)
        NavUtils.NavigateUpFromSameTask(Activity);
}

然后收到错误消息:

  

System.NotSupportedException:无法从本机句柄f3a0​​0019激活 CriminalIntent.CrimePagerActivity 类型的实例

使用Android版本进行编译:API级别21 参考资料:Xamarin.Android.Support.v4,Xamarin.Android.Support.v7.AppCompat


更新1

{System.NotSupportedException: Unable to activate instance of type CriminalIntent.CrimePagerActivity from native handle 37700019 
---> System.MissingMethodException: No constructor found for CriminalIntent.CrimePagerActivity::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) 
---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
Java.Lang.Error: Exception of type 'Java.Lang.Error' was thrown.

  
--- End of managed exception stack trace 
---
java.lang.Error: Java callstack:

	at criminalintent.CrimeFragment.n_onOptionsItemSelected(Native Method)

	at criminalintent.CrimeFragment.onOptionsItemSelected(CrimeFragment.java:56)

	at android.support.v4.app.Fragment.performOptionsItemSelected(Fragment.java:1894)

	at android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:2034)

	at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:356)
		at com.android.internal.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:181)

	at android.view.View.performClick(View.java:4756)

	at android.view.View$PerformClick.run(View.java:19749)

	at android.os.Handler.handleCallback(Handler.java:739)

	at android.os.Handler.dispatchMessage(Handler.java:95)
	
	at android.os.Looper.loop(Looper.java:135)
	
	at android.app.ActivityThread.main(ActivityThread.java:5221)

	at java.lang.reflect.Method.invoke(Native Method)

	at java.lang.reflect.Method.invoke(Method.java:372)

	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


  --- End of inner exception stack trace ---

  at Java.Interop.TypeManager.CreateProxy (System.Type type, IntPtr handle, JniHandleOwnership transfer) [0x00039] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:267
 
  at Java.Interop.TypeManager.CreateInstance (IntPtr handle, JniHandleOwnership transfer, System.Type targetType) [0x001ea] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:249
 
  --- End of inner exception stack trace ---

  at Java.Interop.TypeManager.CreateInstance (IntPtr handle, JniHandleOwnership transfer, System.Type targetType) [0x0022b] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:255
 
  at Java.Lang.Object.GetObject (IntPtr handle, JniHandleOwnership transfer, System.Type type) [0x000ba] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:333
 
  at Java.Lang.Object._GetObject[FragmentActivity] (IntPtr handle, JniHandleOwnership transfer) [0x0001a] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:313
 
  at Java.Lang.Object.GetObject[FragmentActivity] (IntPtr handle, JniHandleOwnership transfer) [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:305
 
  at Android.Support.V4.App.Fragment.get_Activity () [0x00000] in <filename unknown>:0
 
  at CriminalIntent.CrimeFragment.OnOptionsItemSelected (IMenuItem item) [0x0002c] in d:\Xamarin Project\TheBigNerdRanchGuide\CriminalIntent\CrimeFragment.cs:115 }

添加两个构造函数后:

    public CrimePagerActivity()
    {

    }

    public CrimePagerActivity(System.IntPtr i, Android.Runtime.JniHandleOwnership p)
    {

    }

更新2

{Java.Lang.IllegalArgumentException: Exception of type 'Java.Lang.IllegalArgumentException' was thrown.

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0
 
  at Android.Runtime.JNIEnv.CallStaticVoidMethod (IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00063] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1457 
  at Android.Support.V4.App.NavUtils.NavigateUpFromSameTask (Android.App.Activity sourceActivity) [0x00000] in <filename unknown>:0
 
  at CriminalIntent.CrimeFragment.OnOptionsItemSelected (IMenuItem item) [0x0008b] in d:\GitHub\Xamarin.Android\App\TheBigNerdRanchGuide\CriminalIntent\CrimeFragment.cs:200
 
  --- End of managed exception stack trace ---

java.lang.IllegalArgumentException: Activity CrimePagerActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data>  element in your manifest?)

	at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:178)

	at criminalintent.CrimeFragment.n_onOptionsItemSelected(Native Method)

	at criminalintent.CrimeFragment.onOptionsItemSelected(CrimeFragment.java:83)

	at android.support.v4.app.Fragment.performOptionsItemSelected(Fragment.java:1894)

	at android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:2034)

	at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:356)

	at com.android.internal.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:181)

	at android.view.View.performClick(View.java:4756)

	at android.view.View$PerformClick.run(View.java:19749)

	at android.os.Handler.handleCallback(Handler.java:739)

	at android.os.Handler.dispatchMessage(Handler.java:95)

	at android.os.Looper.loop(Looper.java:135)

	at android.app.ActivityThread.main(ActivityThread.java:5221)

	at java.lang.reflect.Method.invoke(Native Method)

	at java.lang.reflect.Method.invoke(Method.java:372)

	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
}

1 个答案:

答案 0 :(得分:1)

有一个很好的指示:错误:CrimePagerActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)

您提供的活动的名称可能有误或包名称不正确。您可以通过查看生成并放入obj\Android文件夹的AndroidManifest来查看生成的名称。

但是在这种情况下的问题很可能是你使用大写的包名,这是Android不喜欢的。如果您查看堆栈跟踪,则会将您的应用中的内容称为criminalintent.xxx,其中xxx是您的片段或活动。

所以不应该写Value = "CriminalIntent.CrimeListActivity",而应该更像Value = "criminalintent.CrimeListActivity"。但你可以在你的清单中看到它。