I created my own SapperButton extends Button class SapperButton extends Button , in a view I’m distributing such buttons of this view. When viewing a designer, a window is displayed with

 The following class could not be found - SapperButton 

And after the launch, when switching to the corresponding layout with this view, everything falls. I understand that I cannot find my successor class from Button . But why and how to fix it?

Here is the beginning of the activity from which the view is called:

 package com.example.helen.mygamesapper; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class GameActivity extends Activity implements View.OnClickListener { int numBombs; Button[][] fields; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); TextView numBombText = (TextView) findViewById(R.id.numBombText); Intent intent = getIntent(); numBombs = intent.getIntExtra("numBombs", 10); 

Here is my button derived class:

 package com.example.helen.mygamesapper; import android.content.Context; import android.util.AttributeSet; import android.widget.Button; public class SapperButton extends Button { private boolean isOpen = false; private boolean isBomb = false; private boolean isBlown = false; /* public SapperButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } */ public SapperButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public SapperButton(Context context, AttributeSet attrs) { super(context, attrs); } public SapperButton(Context context) { super(context); } public boolean isOpen() { return isOpen; } public void setOpen(boolean open) { isOpen = open; } public void setBomb(boolean bomb) { isBomb = bomb; } public boolean getIsBomb() { return isBomb; } public boolean isBlown() { return isBlown; } public void setBlowned(boolean isBlowning) { isBlown = isBlowning; } } 

The view itself:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/numBombs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:weightSum="1.0" android:paddingTop="5pt" android:paddingBottom="5pt" android:background="@android:color/background_dark" android:layout_alignParentTop="true"> <TextView android:id="@+id/numBombText" android:layout_marginTop="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/test" android:textStyle="bold" android:textSize="12sp"> </TextView> </LinearLayout> <LinearLayout android:layout_below="@id/numBombs" android:id="@+id/lin123" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:paddingTop="5pt" android:paddingBottom="5pt" android:weightSum="1.0"> <SapperButton android:layout_width="0dp" android:id="@+id/btn1" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> <SapperButton android:layout_width="0dp" android:id="@+id/btn2" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> <SapperButton android:layout_width="0dp" android:id="@+id/btn3" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> <SapperButton android:layout_width="0dp" android:id="@+id/btn4" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> <SapperButton android:layout_width="0dp" android:id="@+id/btn5" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> <SapperButton android:layout_width="0dp" android:id="@+id/btn6" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> <SapperButton android:layout_width="0dp" android:id="@+id/btn7" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> <SapperButton android:layout_width="0dp" android:id="@+id/btn8" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> <SapperButton android:layout_width="0dp" android:id="@+id/btn9" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> <SapperButton android:layout_width="0dp" android:id="@+id/btn10" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> </LinearLayout> </RelativeLayout> 

Error log:

03-26 16: 24: 52.044 9706-9706 / com.example.helen.mygamesapper E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.helen.mygamesapper, PID: 9706
java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.helen.mygamesapper / com.example.helen.mygamesapper.GameActivity}: android.view.InflateException: Binary XML file line # 44: Error inflating SapperButton at android .app.ActivityThread.performLaunchActivity (ActivityThread.java:2358) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2410) at android.app.ActivityThread.access $ 800 (ActivityThread.java:155) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1331) at android.os.Handler.dispatchMessage (Handler.java:110) at android.os.Looper.loop (Looper.java:193) at android.app.ActivityThread.main ( ActivityThread.java:5395) at java.lang.reflect.Method.invokeNative (Native Method) at java.lang.reflect.Method.invoke (Method.java Curve 15) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller. run (ZygoteInit.java:837) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:653) at dalvik.system.NativeStart.main (Native Method) Caused by: android.view.InflateException: Binary XML file line # 44: Error inflating class SapperButton at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:707) at android.view.LayoutInflater.rInflate (LayoutInflater.java:755) at android.view.LayoutInflater.rInflate (LayoutInflater.java:758) at android.view.LayoutInflater.inflate (LayoutInflater.java:492) at android.view.LayoutInflater.inflate (LayoutInflater.java;97) at android.view.LayoutIntelate .inflate (LayoutInflater.javaUE53) at com.android.internal.policy.impl.PhoneWindow.setContentView (PhoneWindow.java.7312) at android.app.Activity.setContentView (Activity.java:1952) at com.example. helen.mygamesapper.GameActivity.onCreate (GameActivity.java:20) at android.app.Activity.performCreate (Activity.java:5277) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1088) at android.app.ActivityThread .performLaunchActivity (ActivityThread.java:2322) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2410) at android.app.ActivityThread.access $ 800 (ActivityThread.java:15) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1331) at android.os.Handler.dispatchMessage (Handler.java:110) at android.os.Looper.loop (Looper.java:193) at android.app.ActivityThread.main (ActivityThread.java:5395) at java.lang.reflect.Method.invokeNative (Native Method) at java.lang.reflect .Method.invoke (Method.java unde15) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:837) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:653 ) at dalvik.system.NativeStart.main (Native Method) Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.SapperButton" on path: DexPathList [[zip file "/ data / app / com. example.helen.mygamesapper-1.apk "], nativeLibraryDirectories = [/ data / app-lib / com.example.helen.mygamesapper-1, / vendor / lib, / system / lib]] at dalvik.system.BaseDexClassLoader. findClass (BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass (ClassLoade r.java:497) at java.lang.ClassLoader.loadClass (ClassLoader.java:457) at android.view.LayoutInflater.createView (LayoutInflater.java 12.59) at android.view.LayoutInflater.onCreateView (LayoutInflater.java:652 ) at com.android.internal.policy.impl.PhoneLayoutInflater. 694) at android.view.LayoutInflater.rInflate (LayoutInflater.java:755) at android.view.LayoutInflater.rInflate (LayoutInflater.java:758) at android.view.LayoutInflater.inflate (LayoutInflater.java interview92) at android. view.LayoutInflater.inflate (LayoutInflater.java.7397) at android.view.LayoutInflater.inflate (LayoutInflater.java.7353) at com.android.internal.policy.impl.PhoneWindow.setContentView (PhoneWindow.java.0212) at android .app.Activity.setContentView (Activity.java:1952) at com.example.helen.mygamesapper.GameActivity.onCreate (GameActivity.java:20) at android .app.Activity.performCreate (Activity.java:5277) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:10 88) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2322) at android.app.ActivityThread. handleLaunchActivity (ActivityThread.java:2410) at android.app.ActivityThread.access $ 800 (ActivityThread.java:15) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1331) at android.os.Handler.dispatchMessage ( Handler.java:110) at android.os.Looper.loop (Looper.java:193) at android.app.ActivityThread.main (ActivityThread.java:5395) at java.lang.reflect.Method.invokeNative (Native Method) at java.lang.reflect.Method.invoke (Method.java including 15) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:837) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:653) at dalvik.system.NativeStart.main (Native Method)

  • To answer, we need to look at your class, markup, error type. And the buggy preview - do not use it at all. - Yuriy SPb
  • Laid out classes and view - Helen
  • and what error in the logs? - Jarvis_J
  • copied the log - Helen
  • one
    If the answer to the question solved the problem stated in the question, mark it as correct. On a new problem, create a new question, do not edit the old question so that its original meaning is lost. The fact is that this question will help someone else solve a similar problem in the future. If it is corrected with a description of another problem, the question and answer will not correspond and it will be useless in the future. This Q & A resource, not the forum, the purpose of such a resource is to create a base of working reusable solutions, and not be limited to using one person. - pavlofff

1 answer 1

For custom View in the xml markup you need to specify the full path to the class:

  <com.example.helen.mygamesapper.SapperButton android:layout_width="0dp" android:id="@+id/btn1" android:layout_height="wrap_content" android:layout_weight="0.1" android:background="#f1f1c8" android:textColor="#00ecfd" android:gravity="center" /> 
  • Redid the view, now even the build is not created. Log in the main post. - Helen
  • I made the minimum API 16 instead of 15, now everything is displayed in the designer, but now it highlights all the R in red with the Activity, and when you try to knock, it displays the last log from the first post. - Helen
  • for the studio to find R you need to do Build ==> Clean Project - Jarvis_J
  • @Abrog Petrovich did not help - Helen
  • @pavlofff 23 and so it was, the SDK installed everything from version 15, the project rebuilt it, did not help. - Helen