2016-03-26 10 views
0

Ich habe eine class Sapper Button extends Button erstellt, und Schaltflächen dieses Typs sind im Layout. In desiner Fenster sehe ich:Die Anwendung abstürzt in SetcontentView (Meine Aktivität), die meine Klasse verwendet erweitert Schaltfläche

castom view SupperButton is not using the 2- or 3- argument. View constructors. XML attributes will not work. 

Und meine App wird nach dem Start dieses Layout bei setContentView(R.layout.activity_game); abgestürzt. Warum und wie man es repariert? Hier ist der Anfang meiner Aktivität:

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); 

mein Layout:

<?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> 

meine Button-Klasse:

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; 
} 

}

lo g Krachen:

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) 

Antwort

0

Sie haben die Konstrukteure in SapperButton kommentiert, dass Android verwenden muss Instanzen dieser Ansicht, wenn in einem XML-Layout verwendet zu erstellen. Das sind the 2- or 3- argument constructors, über die Sie in der Fehlermeldung informiert werden. Kommentieren Sie sie, überprüfen Sie sie auf Richtigkeit und versuchen Sie es erneut.

+0

Ja, es funktioniert! Vielen Dank. – Helen

Verwandte Themen