当我使用包含选项卡的SetContentView (Resource.Layout.Main)时,我得到Android.

本文关键字:Main Layout Android Resource 包含 选项 SetContentView | 更新日期: 2023-09-27 18:09:24

当我使用包含制表符的setContentView (Resource.Layout.Main)时,我得到一个Android.Views.InflateException,并且无法理解为什么。

我一直试图找到其他人与我的问题,但似乎Android.Views.InflateException是一个非常普遍的错误信息,因为人们似乎在各种情况下得到它。我已经尝试了上面提到的一些方法,但到目前为止它们对我没有帮助。

奇怪的是,完全相同的Main.axml代码在不同的试用应用程序中完美地工作,但我不能让它在这个应用程序中工作。

我在编程应用程序相当新,我不知道它是否有任何区别,但我正在使用Xamarin Studio。我已经尝试了其他一些简单的应用程序来使用所有的新东西,它们工作起来没有太大的麻烦。

这是我的主启动器的代码:

using System;
using System.IO;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
//using Mono.Data.Sqlite;
namespace MultiFuncTest1
{
    [Activity (MainLauncher=true, Label="@string/app_name", Theme="@android:style/Theme.NoTitleBar")]
    public class MultiFuncTest1 : TabActivity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            // Set the view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // ----------------------------------------------------------------------------------------------------
            //                                                  TABS
            // ----------------------------------------------------------------------------------------------------
            // Declares TabSpec and Intent
            TabHost.TabSpec spec;
            Intent intent;
            // Creates and Intent To Start and Activity for the Image-tab.
            intent = new Intent (this, typeof (ImageActivity));
            intent.AddFlags (ActivityFlags.NewTask);
            // Starts a TabSpec for the Image-tab and adds it to the TabHost
            spec = TabHost.NewTabSpec ("image");
            spec.SetIndicator ("Image", Resources.GetDrawable (Resource.Drawable.Icon));
            spec.SetContent (Intent);
            TabHost.AddTab (spec);
            // Creates and Intent To Start and Activity for the Icon-tab.
            intent = new Intent (this, typeof (IconActivity));
            intent.AddFlags (ActivityFlags.NewTask);
            // Starts a TabSpec for the Icon-tab and adds it to the TabHost
            spec = TabHost.NewTabSpec ("icons");
            spec.SetIndicator ("Icons", Resources.GetDrawable (Resource.Drawable.Icon));
            spec.SetContent (Intent);
            TabHost.AddTab (spec);
            // Creates and Intent To Start and Activity for the Image-tab.
            intent = new Intent (this, typeof (ItemListActivity));
            intent.AddFlags (ActivityFlags.NewTask);
            // Starts a TabSpec for the Image-tab and adds it to the TabHost
            spec = TabHost.NewTabSpec ("itemList");
            spec.SetIndicator ("Item List", Resources.GetDrawable (Resource.Drawable.Icon));
            spec.SetContent (Intent);
            TabHost.AddTab (spec);
            // Creates and Intent To Start and Activity for the Image-tab.
            intent = new Intent (this, typeof (WebLinkActivity));
            intent.AddFlags (ActivityFlags.NewTask);
            // Starts a TabSpec for the Image-tab and adds it to the TabHost
            spec = TabHost.NewTabSpec ("webLink");
            spec.SetIndicator ("Web Links", Resources.GetDrawable (Resource.Drawable.Icon));
            spec.SetContent (Intent);
            TabHost.AddTab (spec);
            TabHost.CurrentTab = 1;
        }
    }

}

这是我的ImageActivity的代码,它应该是标签之一。所有的选项卡活动都有非常相似的代码,因为我只是想让大纲工作:

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.Views;
using Android.Widget;
namespace MultiFuncTest1
{
    [Activity (Label = "ImageActivity")]
    public class ImageActivity : Activity
    {
        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            //SetContentView (Resource.Layout.ImagePage);
            TextView textview = new TextView (this);
            textview.Text = "This is the Artists tab";
            SetContentView (textview);
        }
    }
}

我的Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"/>
    </LinearLayout>
</TabHost>

和作为制表符之一的ImagePage.axml。其他选项卡现在看起来非常相似:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ImageView
        android:src="@drawable/Icon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/iPimageView1" />
</LinearLayout>

这是我得到的堆栈跟踪的第一部分。在"托管异常堆栈跟踪结束"部分之后,跟踪会持续很长一段时间,并以"和250多行"或类似的内容结束,所以我认为这些并不重要?如果他们有帮助,告诉我,我也会添加他们:

Android.Views.InflateException: Binary XML file line #24: Error inflating class <unknown>
at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00024] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:616
at Android.App.Activity.SetContentView (int) [0x0006b] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.App.Activity.cs:3119
at MultiFuncTest1.MultiFuncTest1.OnCreate (Android.OS.Bundle) [0x00009] in d:'Mobile App Dev'Projects'MultiFuncTest1'MultiFuncTest1'MultiFuncTest1.cs:24
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.App.Activity.cs:1490
at at (wrapper dynamic-method) object.46f87726-02c8-486d-b95c-c7cce7c7d106 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
at 
at --- End of managed exception stack trace ---

我希望有人能帮助我理解这里的问题,我真的很想掌握我可能做错了什么…

当我使用包含选项卡的SetContentView (Resource.Layout.Main)时,我得到Android.

您的ImagePage。axml不需要TabHost和TabWidget标签;您应该只在main . xml中包含这些。标签的布局将包含在Main的framayout中。因此每个TAB布局文件只有一个

我自己在搜索之后发现了这个问题,它在我的主启动器中。问题是,我试图在"spec.SetContent (Intent);"行中调用类"Intent",同时创建每个类,而我应该调用的是我的实例,"Intent"(没有大写的"I")。所以基本上这是一个拼写错误,但它没有显示为我语法错误,因为有一个类具有相同的名称…

谢谢!

这可能是相当无关的,但谁仍然想知道这个错误可能来自哪里-可能会指出你在正确的方向。

我也有同样的问题,原来我从AXML中引用的自定义控件指定了错误的路径,即控件无处可寻。

我已经遇到这个问题很多次了,

当你没有在你的activity中定义你的THEME时,这个特殊的异常被抛出。你应该有这样的东西

[Activity (Label = "ImageActivity", Theme="@style/yourtheme")]

我已经遇到这个问题很多次了,

当你没有在你的activity中定义你的THEME时,这个特殊的异常被抛出。你应该有这样的东西

[Activity (Label =" ImageActivity", Theme="@styles/yourtheme")]

####在visual studio中style的文件名是styles.xml而不是style.xml