使用layoutInflator xamarin进行充气时的运行时异常?

时间:2015-08-21 16:51:22

标签: c# android visual-studio-2013 xamarin

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/theNewsTile"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="#ffffff">
    <ImageView
        android:id="@+id/newsImage"
        android:src="@drawable/BackGround"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:layout_alignParentBottom="true"
        android:background="#ffffff">
        <TextView
            android:text="MbSoft News App"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:gravity="center_vertical"
            android:textColor="#000000"
            android:layout_marginLeft="8dp"
            android:minWidth="220dp"
            android:layout_gravity="center" />
        <TextView
            android:id="@+id/link"
            android:visibility="gone"
            android:layout_gravity="bottom" />
    </LinearLayout>
</RelativeLayout>

这是我的

保存的axml文件
  

NewsTile.axml

文件 我试图用

来膨胀它
RelativeLayout rl = this.FindViewById<RelativeLayout>(Resource.Id.newsContainer);
                    var viewObj = this.LayoutInflater.Inflate(Resource.Layout.NewsTile,rl,false);
                    viewObj.FindViewById<TextView>(Resource.Id.textView2).Text = rss.Title;
                    TextView tv = viewObj.FindViewById<TextView>(Resource.Id.textView2);
                    Toast.MakeText(this, tv.Text, ToastLength.Long).Show();
                    tv.Text = rss.Title;
                    rl.AddView(viewObj);

我是

的新手
  

XAMARIN

并放养。

任何帮助将不胜感激。先感谢您。 这是调试中可能有用的部分

  

08-21 22:16:15.219 D / Mono(11091):探索&#39; __ android_log_print&#39;。   08-21 22:16:15.229 D / Mono(11091):发现为&#39; __ android_log_print&#39;。   08-21 22:16:15.259 I / MonoDroid(11091):UNHANDLED EXCEPTION:08-21   22:16:15.369 I / MonoDroid(11091):Java.Lang.RuntimeException:异常   类型&#39; Java.Lang.RuntimeException&#39;被扔了。 08-21 22:16:15.369   I / MonoDroid(11091):at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   [0x0000b] in   /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61   08-21 22:16:15.378 I / MonoDroid(11091):at   Android.Runtime.JNIEnv.CallObjectMethod   (intptr,intptr,Android.Runtime.JValue *)[0x00064] in   /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:195   08-21 22:16:15.389 I / MonoDroid(11091):at   Android.Views.LayoutInflater.Inflate   (int,Android.Views.ViewGroup,bool)[0x00082] in   /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Views.LayoutInflater.cs:687   08-21 22:16:15.389 I / MonoDroid(11091):at   NewsApplication.MainActivity.GetFeedItems()[0x000c5] in   c:\ Users \ superfast \ Documents \ Visual Studio   2013 \ Projects \ NewsApplication \ NewsApplication \ MainActivity.cs:62 08-21   22:16:15.409 I / MonoDroid(11091):at   NewsApplication.MainActivity.OnCreate(Android.OS.Bundle)[0x00015] in   c:\ Users \ superfast \ Documents \ Visual Studio   2013 \ Projects \ NewsApplication \ NewsApplication \ MainActivity.cs:33 08-21   22:16:15.409 I / MonoDroid(11091):at   Android.App.Activity.n_OnCreate_Landroid_os_Bundle_   (intptr,intptr,intptr)[0x00011] in   /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2707   08-21 22:16:15.409 I / MonoDroid(11091):at(包装动态方法)   object.9daf293e-e190-4f49-8a44-fd40442dd081(intptr,intptr,intptr)08-21 22:16:15.409 I / MonoDroid(11091):---结束   托管异常堆栈跟踪--- 08-21 22:16:15.409   I / MonoDroid(11091):java.lang.RuntimeException:二进制XML文件行

     

1:您必须提供layout_width属性。 08-21 22:16:15.409 I / MonoDroid(11091):at

     

android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)   08-21 22:16:15.409 I / MonoDroid(11091):at   android.view.ViewGroup $ LayoutParams.setBaseAttributes(ViewGroup.java:5458)   08-21 22:16:15.419 I / MonoDroid(11091):at   android.view.ViewGroup $ MarginLayoutParams。(ViewGroup.java:5594)   08-21 22:16:15.419 I / MonoDroid(11091):at   android.widget.LinearLayout $的LayoutParams。(LinearLayout.java:1809)   08-21 22:16:15.419 I / MonoDroid(11091):at   android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1721)   08-21 22:16:15.419 I / MonoDroid(11091):at   android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:58)   08-21 22:16:15.419 I / MonoDroid(11091):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:748)08-21   22:16:15.419 I / MonoDroid(11091):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:749)08-21   22:16:15.419 I / MonoDroid(11091):at   android.view.LayoutInflater.inflate(LayoutInflater.java:489)08-21   22:16:15.419 I / MonoDroid(11091):at   android.view.LayoutInflater.inflate(LayoutInflater.java:396)08-21   22:16:15.419 I / MonoDroid(11091):at   md52a660c74cd716f3b9a164dea37c81895.MainActivity.n_onCreate(母语   方法)08-21 22:16:15.419 I / MonoDroid(11091):at   md52a660c74cd716f3b9a164dea37c81895.MainActivity.onCreate(MainActivity.java:28)   08-21 22:16:15.419 I / MonoDroid(11091):at   android.app.Activity.performCreate(Activity.java:5008)08-21   22:16:15.429 I / MonoDroid(11091):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)   08-21 22:16:15.429 I / MonoDroid(11091):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)   08-21 22:16:15.429 I / MonoDroid(11091):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)   08-21 22:16:15.429 I / MonoDroid(11091):at   android.app.ActivityThread.access $ 600(ActivityThread.java:130)08-21   22:16:15.429 I / MonoDroid(11091):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)   08-21 22:16:15.429 I / MonoDroid(11091):at   android.os.Handler.dispatchMessage(Handler.java:99)08-21 22:16:15.429   I / MonoDroid(11091):在android.os.Looper.loop(Looper.java:137)08-21   22:16:15.429 I / MonoDroid(11091):at   android.app.ActivityThread.main(ActivityThread.java:4745)08-21   22:16:15.439 I / MonoDroid(11091):at   java.lang.reflect.Method.invokeNative(Native Method)08-21   22:16:15.439 I / MonoDroid(11091):at   java.lang.reflect.Method.invoke(Method.java:511)08-21 22:16:15.439   I / MonoDroid(11091):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)   08-21 22:16:15.439 I / MonoDroid(11091):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)08-21   22:16:15.439 I / MonoDroid(11091):at   dalvik.system.NativeStart.main(Native Method)08-21 22:16:15.509   D / AndroidRuntime(11091):关闭VM 08-21 22:16:15.509   W / dalvikvm(11091):threadid = 1:线程退出未捕获的异常   (group = 0x40a13300)发生了未处理的异常。

1 个答案:

答案 0 :(得分:0)

每个元素都需要android:layout_widthandroid:layout_height属性。你布局中的最后一个TextView缺少它们。