【问题标题】:No view found for id with LinearLayout and MapView使用 LinearLayout 和 MapView 找不到 id 的视图
【发布时间】:2019-07-12 00:38:14
【问题描述】:

我收到一个错误提示

java.lang.IllegalArgumentException:未找到 id 0x7f0e00a7 的视图 (com.jvlapps.myapp:id/linearLayout1) 用于片段 ParamMapFragment

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linearLayout1">
    <com.google.android.gms.ads.AdView
        android:id="@+id/bannerAdView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@+string/AdMob_TEST_Unit_Id" />
 <fragment
        android:name="GetMeThere.MainMenuFragment"
        android:id="@+id/MainMenuFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

这里是 ParamMapFragment.cs:

public class ParamMapFragment : Fragment
    {
        public string AgencyName => Arguments.GetString("current_agency", "None");

        public ParamMapFragment() { }

        public static ParamMapFragment NewInstance(string currentAgency)
        {
            var bundle = new Bundle();
            bundle.PutString("current_agency", currentAgency);
            return new ParamMapFragment { Arguments = bundle };
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            if (container == null)
            {
                return null;
            }

            MapView mapView = new MapView(Activity);

            return mapView;
        }

    }

这是我的 Activity 的 OnCreate:

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

    var agencyName = Intent.Extras.GetString("current_agency", "None");

    var mapFrag = ParamMapFragment.NewInstance(agencyName);
    FragmentManager.BeginTransaction()
                    .Add(Resource.Id.linearLayout1, mapFrag, "main_map_fragment")
                    .Commit();
}

我已经为此苦苦挣扎了好几个小时。我不知道此时我什至在做什么,或者我的应用程序应该再做什么。我要完成这个应用程序,然后毁灭宇宙。

【问题讨论】:

  • 你在哪一行得到这个错误?
  • @Mehmed VS 说“所有线程都在执行外部代码”,所以我没有具体说明。但是 FragmentManager.BeginTransaction().Add... 行是它发生的地方,基于我一次通过一行。

标签: c# android android-fragments xamarin


【解决方案1】:

在添加片段之前,您需要在活动的OnCreate 方法中调用SetContentView(Resource.Layout.&lt;resource id of XML above&gt;);。否则,您的 Activity 没有任何内容视图,也无法找到 id/linearLayout1 的任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 2013-02-20
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多