【发布时间】:2013-04-16 15:34:40
【问题描述】:
当我使用包含选项卡的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 ---
我希望有人可以帮助我理解这里的问题,我真的很想了解我可能做错了什么......
【问题讨论】:
-
它的 R.Layout.main 不是资源。
-
@Arju 作者没有使用 Java。所以代码可能会有所不同。
-
抱歉没有标签为java
-
很抱歉没有在我的问题中解释这一点,但不,我没有使用 Java。 Xamarin studio 基于 Mono,它使用 C# 创建跨平台应用程序。
-
在尝试了一些不同的方法来解决这个问题之后,我至少发现它似乎是“资源”的某种错误。问题是,在我之前的所有应用程序试验中,我都以相同的方式使用了 Resource。当我将 Main.axml 更改为常规 LinearLayout 时,它可以工作,并且它可以在带有选项卡的不同应用程序中工作,但在此处不工作。当我调试时,我得到“未知标识符:资源”。我也尝试过使用 R 或 Resources,但它不起作用......
标签: c# android android-layout xamarin.android xamarin-studio