原因:java.lang.NoSuchMethodException:<;初始化>;[类android.content.Context,接口android.util.AttributeSet]

原因:java.lang.NoSuchMethodException:<;初始化>;[类android.content.Context,接口android.util.AttributeSet],android,android-studio,Android,Android Studio,我在运行应用程序时收到此错误 错误: Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] at java.lang.Class.getConstructorOrMethod(Class.java:472) at java.lang.Class.get

我在运行应用程序时收到此错误

错误:

 Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
            at java.lang.Class.getConstructorOrMethod(Class.java:472)
            at java.lang.Class.getConstructor(Class.java:446)
            at android.view.LayoutInflater.createView(LayoutInflater.java:574)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366) 
            at android.app.Activity.setContentView(Activity.java:2031) 
            at com.example.syafiq.facialrecognition.Recognize_Face.onCreate(Recognize_Face.java:152)
我的活动\u recognize\u face.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e7bc3a">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="0.88"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.88">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <Button
            android:id="@+id/buttonCat"
            android:layout_width="65dp"
            android:layout_height="68dp"
            android:text="@string/SViewAll"
            android:background="#4892f4"/>

            <com.example.syafiq.facialrecognition.Tutorial3View
                android:id="@+id/tutorial3_activity_java_surface_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.88" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="120dp"
                android:layout_height="140dp"
                android:src="@drawable/lightbulb" />
        </LinearLayout>

    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textViewState"
            android:layout_width="190dp"
            android:layout_height="wrap_content"
            android:text="@string/SIdle"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_green" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_yellow" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_red" />

        </RelativeLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.07"
            android:checked="false"
            android:text="@string/STrain"
            android:textOff="@string/STrain"
            android:textOn="@string/SStopTrain"
            android:background="#4892f4" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.71"
            android:text="@string/SFaceName"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.76"
            android:maxLines="1"
            android:singleLine="true">

            <requestFocus />
        </EditText>

        <ToggleButton
            android:id="@+id/buttonBuscar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/SSearch"
            android:textOff="@string/SSearch"
            android:textOn="@string/SStopSearch"
            android:background="#4892f4" />

        <ToggleButton
            android:id="@+id/toggleButtonGrabar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Grabar"
            android:textOff="Rec"
            android:textOn="Stop"
            android:background="#4892f4" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.73"
            android:src="@drawable/ic_stat_camera" />

       </LinearLayout>

     </LinearLayout>
</RelativeLayout>


有人能帮我吗?:我真的很感谢你花时间和帮助我解决我的错误。提前谢谢

您的布局XML中引用了一个自定义视图,而自定义视图没有所需的
public
构造函数,该构造函数接受
上下文、AttributeSet
参数。

如果您在本文中使用Kotlin,请使用
@JvmOverloads
注释您的构造函数

文件:


例如:
class MyView@JVM重载构造函数(context:context,attrs:AttributeSet?=null,defStyle:Int=0):LinearLayout(context,attrs,defStyle)

Hi laalto:)我不明白你的意思,我是android开发新手,对不起:/但我在你的代码中包含了我的xml文件:,
Tutorial3View
没有布局膨胀所需的2-arg构造函数
Tutorial3View(Context,AttributeSet)
。请记住,这些构造函数必须是公共的。@David您让我在
public
上。多谢!我的应用程序仅在发布版本中崩溃。罪犯失踪了。
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e7bc3a">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="0.88"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.88">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <Button
            android:id="@+id/buttonCat"
            android:layout_width="65dp"
            android:layout_height="68dp"
            android:text="@string/SViewAll"
            android:background="#4892f4"/>

            <com.example.syafiq.facialrecognition.Tutorial3View
                android:id="@+id/tutorial3_activity_java_surface_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.88" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="120dp"
                android:layout_height="140dp"
                android:src="@drawable/lightbulb" />
        </LinearLayout>

    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textViewState"
            android:layout_width="190dp"
            android:layout_height="wrap_content"
            android:text="@string/SIdle"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_green" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_yellow" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_red" />

        </RelativeLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.07"
            android:checked="false"
            android:text="@string/STrain"
            android:textOff="@string/STrain"
            android:textOn="@string/SStopTrain"
            android:background="#4892f4" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.71"
            android:text="@string/SFaceName"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.76"
            android:maxLines="1"
            android:singleLine="true">

            <requestFocus />
        </EditText>

        <ToggleButton
            android:id="@+id/buttonBuscar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/SSearch"
            android:textOff="@string/SSearch"
            android:textOn="@string/SStopSearch"
            android:background="#4892f4" />

        <ToggleButton
            android:id="@+id/toggleButtonGrabar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Grabar"
            android:textOff="Rec"
            android:textOn="Stop"
            android:background="#4892f4" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.73"
            android:src="@drawable/ic_stat_camera" />

       </LinearLayout>

     </LinearLayout>
</RelativeLayout>