我是Android的新手,我正在尝试按照教程创建一个简单的应用程序,但是当我尝试使用Android Studio中的模拟器打开该应用程序时,它崩溃并永远不会打开。在logcat中,我在“ ------崩溃开始”之后得到了这个
2018-09-27 14:59:36.362 5807-5807/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: corner.yabej.corner12, PID: 5807
java.lang.RuntimeException: Unable to start activity ComponentInfo{corner.yabej.corner12/corner.yabej.corner12.MainActivity}: android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class info.hoang8f.widget.FButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class info.hoang8f.widget.FButton
Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class info.hoang8f.widget.FButton
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at corner.yabej.corner12.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 1 to color: type=0x5
at android.content.res.TypedArray.getColor(TypedArray.java:463)
at info.hoang8f.widget.FButton.parseAttrs(FButton.java:116)
at info.hoang8f.widget.FButton.<init>(FButton.java:52)
我在主活动XMl中有此提示
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/twelve"
android:layout_width="200dp"
android:layout_height="200dp" />
<TextView
android:gravity="center"
android:id="@+id/txtSlogan"
android:text="@string/slogan"
android:textColor="@android:color/white"
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<info.hoang8f.widget.FButton
android:id="@+id/btnSignUp"
android:text="Sign Up"
android:textColor="@android:color/white"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:buttonColor="@color/btnSignUp"
app:shadowColor="@android:color/black"
app:shadowEnabled="true"
app:shadowHeight="5dp"
app:cornerRadius="4dp"
/>
<info.hoang8f.widget.FButton
android:id="@+id/btnSignIn"
android:text="Sign In"
android:textColor="@android:color/white"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:buttonColor="@color/btnSignActive"
app:shadowColor="@android:color/black"
app:shadowEnabled="true"
app:shadowHeight="5dp"
app:cornerRadius="4dp"
/>
</LinearLayout>
</RelativeLayout>
任何人都知道发生了什么事吗?我完成了所有操作,就像本教程一样。
答案 0 :(得分:1)
错误指向xml文件中的第37行,并且按钮的ID为“ Map (*ps)[16]
”:
btnSignUp
您是否已将此依赖性包含在Error inflating class info.hoang8f.widget.FButton
文件中?
gradle.build
此外,请确保将dependencies {
compile 'info.hoang8f:fbutton:1.0.5'
}
类从Github复制到您的项目中。最后,请确保已在“值”文件夹中设置了颜色。
答案 1 :(得分:0)
尝试删除FButton中的buttonColor属性,因为真正的错误是
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 1 to color: type=0x5
at android.content.res.TypedArray.getColor(TypedArray.java:463)
at info.hoang8f.widget.FButton.parseAttrs(FButton.java:116)
at info.hoang8f.widget.FButton.<init>(FButton.java:52)
答案 2 :(得分:0)
错误是由以下代码引起的:
mButtonColor = typedArray.getColor(attr, R.color.fbutton_default_color);
,错误是:
java.lang.UnsupportedOperationException:无法转换索引处的值 1表示颜色:类型= 0x5
因此,您应确保颜色正确。也许您没有将#
设置为颜色...