Xamarin Android popupwindow - 充气时崩溃

时间:2014-10-10 08:28:44

标签: xamarin xamarin.android clock popupwindow countdowntimer

我试图创建一个显示时钟的弹出窗口(实际上我想要的是一个倒数计时器,但一次只有一件事!),麻烦的是当我调用方法来显示弹出窗口时包含时钟的窗口,应用程序崩溃。

从Activity而不是片段调用该方法。

以下是代码:

public void clockPopup()
{

    LayoutInflater inflater =(LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);

    View myPopup = inflater.Inflate (Resource.Layout.PopUpClockTimer, null);

    RunOnUiThread (() => {
        PopupWindow popup = new PopupWindow (myPopup);
    });

        }

堆栈跟踪如下:

[mono] Unhandled Exception:
[mono] Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
[mono]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
[mono]   at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0 
[mono]   at Android.Views.LayoutInflater.Inflate (Int32 resource, Android.Views.ViewGroup root) [0x00000] in <filename unknown>:0 
[mono]   at FrogPoint.Droid.MainActivity.clockPopup () [0x00020] in /Users/jakememery/Desktop/working Frogpoint/FrogPoint1.2_database/FindTheMonkey.Droid/MainActivity.cs:1548 
[mono]   at FrogPoint.Droid.MainActivity+<getBeaconData>c__async3.MoveNext () [0x00258] in /Users/jakememery/Desktop/working Frogpoint/FrogPoint1.2_database/FindTheMonkey.Droid/MainActivity.cs:1215 
[mono] --- End of stack trace from previous location where exception was thrown ---
[mono]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
[mono]   at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <filename unknown>:0 
[mono]   at FrogPoint.Droid.MainActivity+<RangingBeaconsInRegion>c__async0.MoveNext () [0x0035d] in /Users/jakememery/Desktop/working Frogpoint/FrogPoint1.2_database/FindTheMonkey.Droid/MainActivity.cs:675 
[mono]   --- End of managed exception stack trace ---
[mono] android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.AnalogClock
[mono]  at android.view.LayoutInflater.createView(LayoutInflater.java:620)
[mono]  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
[mono]  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
[mono]  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
[mono]  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
[mono]  at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
[mono]  at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
[mono]  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
[mono]  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
[mono]  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
[mono]  at dalvik.system.NativeStart.run(Native Method)
[mono] Caused by: java.lang.reflect.InvocationTargetException
[mono]  at java.lang.reflect.Constructor.constructNative(Native Method)
[mono]  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[mono]  at android.view.LayoutInflater.createView(LayoutInflater.java:594)
[mono]  ... 10 more
[mono] Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
[mono]  at android.os.Handler.<init>(Handler.java:200)
[mono]  at android.os.Handler.<init>(Handler.java:114)
[mono]  at android.widget.AnalogClock.<init>(AnalogClock.java:57)
[mono]  at android.widget.AnalogClock.<init>(AnalogClock.java:67)

以下是我试图充气的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/customborder"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:orientation="vertical">
            <TextView
                android:id="@+id/textout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Time sensitive offer" />
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/logo76" />
            <AnalogClock
                android:id="@+id/tabAnalogClock"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <DigitalClock
                android:id="@+id/tabDigitalClock"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            <Button
                android:id="@+id/dismiss"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Dismiss" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

有人能看到我在哪里出错了吗?

2 个答案:

答案 0 :(得分:0)

似乎我只需要调用这样的方法:

RunOnUiThread (() => {
    clockPopup();
});

现在不会崩溃。但不幸的是它也没有显示弹出窗口。但这是另一篇文章。

答案 1 :(得分:0)

你错过了:

popup.ShowAtLocation(...);

popup.ShowAsDropDown(...);

实际显示弹出窗口。