Создала свой класс SapperButton extends Button, во вьюхе накидала таких кнопок этого вида. При просмотре дезайнера выдается окно с
The following class could not be found - SapperButton
А после запуска при переходе на соответствующий лейоут с этой вьюхой все падает.
Понимаю, что не может найти мой класс-наследник от Button. Но почему и как это исправить?
Вот начало активити, из которого вызывается вьюха:
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);
Вот мой класс-наследник от кнопки:
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;
}
}
Сама вьюха:
Лог ошибки:
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 class 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:515)
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:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:312)
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: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:515)
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(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.view.LayoutInflater.createView(LayoutInflater.java:559)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
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:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:312)
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: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:515)
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)
Ответ
Для кастомных View в разметке xml нужно указывать полный путь до класса:
Комментариев нет:
Отправить комментарий