Xamarin - 尝试启动主活动外的活动时出现空指针异常?

时间:2015-02-25 23:14:27

标签: android android-activity android-fragments xamarin

我有一个应用程序,这是第一个代码块

public class MainActivity : Activity
{

    protected override void OnCreate(Bundle oBundle)
    {
        base.OnCreate(oBundle);

        SetContentView(Resource.Layout.Layout_Main);

        FragmentTransaction oFragmentTransaction = FragmentManager.BeginTransaction();
        SlidingTabsFragment oSlidingTabsFragment = new SlidingTabsFragment();

        oFragmentTransaction.Replace(Resource.Id.SampleContentFragment, oSlidingTabsFragment);

        oFragmentTransaction.Commit();
    }

此应用有一个Swiping标签布局,我创建了一个自定义滑动标签片段。这是SlidingTabsFragment类代码

public class SlidingTabsFragment : Fragment
{
    public override View OnCreateView(LayoutInflater oLayoutInflater, ViewGroup oViewGroup, Bundle savedInstanceState)
    {
        return oLayoutInflater.Inflate(Resource.Layout.Layout_SlidingLayoutBase, oViewGroup, false);
    }

    public override void OnViewCreated(View oView, Bundle savedInstanceState)
    {
        SlidingTabScrollView oSlidingTabScrollView = oView.FindViewById<SlidingTabScrollView>(Resource.Id.SlidingLayout_SlidingTabScrollView);

        ViewPager oViewPager = oView.FindViewById<ViewPager>(Resource.Id.SlidingLayout_ViewPager);

        oViewPager.Adapter = new TabPageAdapter();

        oSlidingTabScrollView.ViewPager = oViewPager;
    }
}

所以在SlidingTabFragment上继承自Fragment - 在创建视图上,它会扩展布局,onViewCreated它会设置SlidingTabScrollView,ViewPager和TabPageAdapter。标签页适配器有一些代码来处理TabItem内容。

所以这是我的问题:

在TabItems上有一个listview - 我正在收听一个OnClick事件。当我发现这个事件时,目标是开始新的活动。

我得到了一个Java.Lang.NullPointerException,就在这里。我已经尝试在清单文件中添加活动名称,但它没有什么区别。我至少经历了10篇关于它的帖子,并且无法知道为什么不启动它。如果我抓住一个ButtonBackPRess并在那里插入我的代码段就可以了。

Intent oIntent = new Intent(this, typeof(SignInActivity));
        StartActivity(oIntent);

正是这个TabLayout和不同的类不让我看到正在发生的事情的清晰画面。为什么我不能开始活动研究员?

  Java.Lang.NullPointerException: 
at at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068>
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:896
  at Android.Content.ContextWrapper.StartActivity (Android.Content.Intent) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Content.ContextWrapper.cs:2895
  at MyAppName.TabLayouts..ctor (Android.Views.ViewGroup) [0x0003d] in c:\Xamarin\MyAppName\MyAppName\TabLayouts.cs:41
  at MyAppName.TabPageAdapter.InstantiateItem (Android.Views.ViewGroup,int) [0x00001] in c:\Xamarin\MyAppName\MyAppName\TabPageAdapter.cs:44
  at at Android.Support.V4.View.PagerAdapter.n_InstantiateItem_Landroid_view_ViewGroup_I (intptr,intptr,intptr,int) <IL 0x00014, 0x000ff>
  at at (wrapper dynamic-method) object.19196691-50f4-44b8-a7cd-7a7ffc0f8179 (intptr,intptr,intptr,int) <IL 0x0001d, 0x0004f>
  at --- End of managed exception stack trace ---
  at java.lang.NullPointerException
  at at android.app.Activity.startActivityForResult(Activity.java:3511)
  at at android.app.Activity.startActivityForResult(Activity.java:3472)
  at at android.app.Activity.startActivity(Activity.java:3714)
  at at android.app.Activity.startActivity(Activity.java:3682)
  at at MyAppName.TabPageAdapter.n_instantiateItem(Native Method)
  at at MyAppName.TabPageAdapter.instantiateItem(TabPageAdapter.java:47)
  at at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
  at at android.support.v4.view.ViewPager.populate(ViewPager.java:987)
  at at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
  at at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
  at at android.view.View.measure(View.java:17387)
  at at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
  at at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
  at at android.view.View.measure(View.java:17387)
  at at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
  at at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  at at android.view.View.measure(View.java:17387)
  at at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
  at at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
  at at android.view.View.measure(View.java:17387)
  at at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
  at at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  at at android.view.View.measure(View.java:17387)
  at at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
  at at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
  at at android.view.View.measure(View.java:17387)
  at at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
  at at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  at at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2533)
  at at android.view.View.measure(View.java:17387)
  at at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2211)
  at at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1350)
  at at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1547)
  at at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1234)
  at at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6465)
  at at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
  at at android.view.Choreographer.doCallbacks(Choreographer.java:603)
  at at android.view.Choreographer.doFrame(Choreographer.java:573)
  at at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
  at at android.os.Handler.handleCallback(Handler.java:733)
  at at android.os.Handler.dispatchMessage(Handler.java:95)
  at at android.os.Looper.loop(Looper.java:157)
  at at android.app.ActivityThread.main(ActivityThread.java:5356)
  at at java.lang.reflect.Method.invokeNative(Native Method)
  at at java.lang.reflect.Method.invoke(Method.java:515)
  at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
  at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
  at at dalvik.system.NativeStart.main(Native Method)

清单文件

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="MyAppName.MyAppName" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="15" />
    <application android:label="MyAppName" android:icon="@drawable/Icon" android:theme="@style/CustomActionBarTheme"></application>
    <activity android:name=".TestActivity"/>
    <activity android:name=".MainActivity"/>
    <activity android:name=".SignInActivity"/>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

2 个答案:

答案 0 :(得分:0)

您不会显示启动活动的代码,我怀疑问题出在您的行中this

Intent oIntent = new Intent(this, typeof(SignInActivity));

我怀疑你没有在那里传递父Activity的上下文。尝试更改它,以便您在意图中使用“活动”,而不是您正在使用的视图/标签的上下文。

(也许NullPointerException是误导性的,因为Xamarin在内部做了一些事情来包装上下文。)

答案 1 :(得分:0)

在处理碎片时,您必须知道一些限制。

首先:您只能替换已动态添加的片段。

第二:你必须定义一个&#34;占位符&#34;布局,这将是您插入(添加)和替换片段的重点。

检查Resource.Id.SampleContentFragment中是否存在Resource.Layout.Layout_Main 同样适用于SlidingTabsFragment

希望这有用。