Snackbar $ SnackbarLayout.onFinishInflate问题

时间:2017-01-16 09:00:35

标签: android xamarin.android

我在Xamarin.Android中尝试使用 Snackbar 时遇到问题

这里是错误堆栈

Java.Lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.Button

任何人都可以指导我为什么会提出这个问题

我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/login_layout">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/pumpkin_orange"
        android:id="@+id/loginToolBar">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ReadyApps.Xamarin.Framework.Droid.Controls.ExtImageView
                android:id="@+id/closeImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/ic_arrow_back_white_24dp"
                android:layout_centerVertical="true" />
            <ReadyApps.Xamarin.Framework.Droid.Controls.ExtTextView
                android:id="@+id/toolbarTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/TextStyle7"
                android:layout_toRightOf="@+id/closeImageView"
                android:layout_toEndOf="@+id/closeImageView"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp"
                android:text="@string/signin"
                android:layout_centerVertical="true" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:layout_gravity="center_horizontal">
        <ReadyApps.Xamarin.Framework.Droid.Controls.ExtImageView
            android:src="@drawable/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:id="@+id/imageView1"
            android:layout_centerHorizontal="true" />
        <LinearLayout
            android:id="@+id/layout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:layout_below="@+id/imageView1">
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColorHint="@color/orange_two">
                <ReadyApps.Xamarin.Framework.Droid.Controls.ExtEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="13.3sp"
                    android:hint="@string/storecode"
                    android:id="@+id/login_storeCode"
                    android:drawablePadding="7.3dp"
                    android:tag="Required" />
            </android.support.design.widget.TextInputLayout>
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColorHint="@color/orange_two">
                <ReadyApps.Xamarin.Framework.Droid.Controls.ExtEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="13.3sp"
                    android:hint="@string/username_email"
                    android:id="@+id/login_userName"
                    android:tag="Required"
                    android:drawablePadding="7.3dp" />
            </android.support.design.widget.TextInputLayout>
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColorHint="@color/orange_two">
                <ReadyApps.Xamarin.Framework.Droid.Controls.ExtEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="13.3sp"
                    android:inputType="textPassword"
                    android:hint="@string/password"
                    android:id="@+id/login_password"
                    android:tag="Required"
                    android:drawablePadding="7.3dp" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>
        <ReadyApps.Xamarin.Framework.Droid.Controls.ExtTextView
            android:text="@string/signin"
            android:layout_marginTop="50dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/signIn"
            android:layout_below="@+id/layout"
            android:textAllCaps="true"
            android:layout_centerHorizontal="true"
            android:textSize="14.7sp"
            android:padding="5dp"
            style="@style/TextStyle11" />

      <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginTop="50dp"
           android:layout_marginLeft="40dp"
           android:layout_marginRight="40dp"
           android:layout_below="@+id/signIn">
        <ReadyApps.Xamarin.Framework.Droid.Controls.ExtTextView
            android:id="@+id/login_forgetpassword"
            android:text="@string/forgetpassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/TextStyle12" />
        <ReadyApps.Xamarin.Framework.Droid.Controls.ExtTextView
              android:id="@+id/login_signUp"
              android:text="@string/signup"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
               android:textAllCaps="true"
              android:layout_alignParentRight="true"
              android:layout_alignParentEnd="true"
              style="@style/TextStyle12"
                />
      </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

我的代码

public class LoginActivity : BaseActivity<LoginActivityLogic>
    {

     private ExtEditText et_vendorCode, et_userName, et_password;
    private ExtTextView tv_forgetpassword , tv_signUp , bt_signIn;    
    private LinearLayout login_layout;
    private string vendorCode;

      protected override void OnCreate(Bundle savedInstanceState)
      {
       SetContentView(Resource.Layout.Login);
       base.OnCreate(savedInstanceState);

      initView();
       }

    private void initView()
    {
        login_layout = FindViewById<LinearLayout>(Resource.Id.login_layout);
        tv_signUp = FindViewById<ExtTextView>(Resource.Id.login_signUp);
        bt_signIn = FindViewById<ExtTextView>(Resource.Id.signIn);
        tv_forgetpassword = FindViewById<ExtTextView>(Resource.Id.login_forgetpassword);
        et_vendorCode = FindViewById<ExtEditText>(Resource.Id.login_storeCode);
        et_userName = FindViewById<ExtEditText>(Resource.Id.login_userName);

        et_password = FindViewById<ExtEditText>(Resource.Id.login_password);

        tv_forgetpassword.Click += Tv_forgetpassword_Click;
        bt_signIn.Click += Bt_signIn_Click;
        et_vendorCode.Text = vendorCode;

        tv_signUp.Click += Tv_signUp_Click;

        ExtImageView iv_Close = FindViewById<ExtImageView>(Resource.Id.closeImageView);
        iv_Close.Click += delegate { Finish(); };

        // android: drawableLeft = "@drawable/ic_business_bluey_grey_24dp"
        et_vendorCode.SetCompoundDrawablesWithIntrinsicBounds(Resource.Drawable.ic_business_bluey_grey_24dp , 0 , 0 , 0);

        //android: drawableLeft = "@drawable/ic_account_blue_grey_24dp"
        et_userName.SetCompoundDrawablesWithIntrinsicBounds(Resource.Drawable.ic_account_blue_grey_24dp, 0, 0, 0);

        //android: drawableLeft = "@drawable/ic_lock_blue_grey_24dp"
        et_password.SetCompoundDrawablesWithIntrinsicBounds(Resource.Drawable.ic_lock_blue_grey_24dp, 0, 0, 0);

    }

// I call it in method

 Snackbar.Make(login_layout, "mystring", Snackbar.LengthShort).Show();


}

但我收到了错误

我的错误日志

java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.Button
android.support.design.widget.Snackbar$SnackbarLayout.onFinishInflate()Snackbar.java:696
android.view.LayoutInflater.rInflate()LayoutInflater.java:814
android.view.LayoutInflater.inflate()LayoutInflater.java:504
android.view.LayoutInflater.inflate()LayoutInflater.java:414
android.support.design.widget.Snackbar.<init>()Snackbar.java:188
android.support.design.widget.Snackbar.make()Snackbar.java:215
mono.java.lang.RunnableImplementor.n_run(Native Method)
mono.java.lang.RunnableImplementor.run()RunnableImplementor.java:30
android.os.Handler.handleCallback()Handler.java:739
android.os.Handler.dispatchMessage()Handler.java:95
android.os.Looper.loop()Looper.java:135
android.app.ActivityThread.main()ActivityThread.java:5221
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke()Method.java:372
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:899
com.android.internal.os.ZygoteInit.main()ZygoteInit.java:694
Xamarin caused by: Java.Lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.Button
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()exceptionservicescommon.cs:143
Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference type, JniMethodInfo method, JniArgumentValue* args)JniEnvironment.g.cs:12649
Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr jclass, IntPtr jmethod, JValue* parms)JNIEnv.g.cs:562
Android.Support.Design.Widget.Snackbar.Make(View view, ICharSequence text, int duration)<0d997552ddad494094d94e2a353b2022>:0
Android.Support.Design.Widget.Snackbar.Make(View view, string text, int duration)<0d997552ddad494094d94e2a353b2022>:0
DSP.Droid.Activities.Orders.LoginActivity.AfterSaveResponse(object sender, EventArgs args)
DSP.Core.UILogic.Orders.LoginActivityLogic.<SignIn>d__1.MoveNext()
Android.App.SyncContext.<Post>c__AnonStorey0.<>m__0()SyncContext.cs:18
Java.Lang.Thread.RunnableImplementor.Run()Thread.cs:36
Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this)Java.Lang.IRunnable.cs:81
at (wrapper dynamic-method) System.Object:4ae9575e-e099-4bc0-bb97-0bb4377e913d (intptr,intptr)
--- End of managed Java.Lang.ClassCastException stack trace ---
java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.Button
android.support.design.widget.Snackbar$SnackbarLayout.onFinishInflate()Snackbar.java:696
android.view.LayoutInflater.rInflate()LayoutInflater.java:814
android.view.LayoutInflater.inflate()LayoutInflater.java:504
android.view.LayoutInflater.inflate()LayoutInflater.java:414
android.support.design.widget.Snackbar.<init>()Snackbar.java:188
android.support.design.widget.Snackbar.make()Snackbar.java:215
mono.java.lang.RunnableImplementor.n_run(Native Method)
mono.java.lang.RunnableImplementor.run()RunnableImplementor.java:30
android.os.Handler.handleCallback()Handler.java:739
android.os.Handler.dispatchMessage()Handler.java:95
android.os.Looper.loop()Looper.java:135
android.app.ActivityThread.main()ActivityThread.java:5221
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke()Method.java:372
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:899
com.android.internal.os.ZygoteInit.main()ZygoteInit.java:694

1 个答案:

答案 0 :(得分:0)

您将在旧版本中遇到的运行时出现此随机错误

如何修复?升级到支持v25或更高版本库