我创建了一个class Sapper Button extends Button
,此类型的按钮位于布局中。在desiner窗口,我看到:
castom view SupperButton is not using the 2- or 3- argument. View constructors. XML attributes will not work.
在setContentView(R.layout.activity_game);
开始此布局后,我的应用程序崩溃了。为什么以及如何解决它?
这是我的活动的开始:
package com.example.helen.mynewgamesapper;
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);
numBombText.setText("Number of bombs is: " + numBombs);
我的布局:
<?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">
<com.example.helen.mynewgamesapper.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"
/>
<com.example.helen.mynewgamesapper.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"
/>
<com.example.helen.mynewgamesapper.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"
/>
<com.example.helen.mynewgamesapper.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"
/>
<com.example.helen.mynewgamesapper.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"
/>
<com.example.helen.mynewgamesapper.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"
/>
<com.example.helen.mynewgamesapper.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"
/>
<com.example.helen.mynewgamesapper.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"
/>
<com.example.helen.mynewgamesapper.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"
/>
<com.example.helen.mynewgamesapper.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>
我的Button类:
package com.example.helen.mynewgamesapper;
import android.content.Context;
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 19:03:58.874 31921-31921/com.example.helen.mynewgamesapper E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.example.helen.mynewgamesapper, PID:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helen.mynewgamesapper/com.example.helen.mynewgamesapper.GameActivity}: android.view.InflateException: Binary XML file line #42: Error inflating class com.example.helen.mynewgamesapper.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 #42: Error inflating class com.example.helen.mynewgamesapper.SapperButton
at android.view.LayoutInflater.createView(LayoutInflater.java:603)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
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.mynewgamesapper.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.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:568)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
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.mynewgamesapper.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)
答案 0 :(得分:0)
你已经注释掉了SapperButton中的构造函数,当在XML布局中使用时,Android需要使用它来创建该视图的实例。这些是the 2- or 3- argument constructors
,它会在错误消息中告诉您。取消注释,检查它们的正确性,然后重试。
答案 1 :(得分:0)
当您浏览视图表单XML时,将调用此构造函数。
public SapperButton(Context context, AttributeSet attrs) {
super(context, attrs);
}