对象未被绘制

时间:2016-06-16 18:32:30

标签: java android

我想绘制一个图像,但每次运行我的应用程序时,它都会崩溃。它说,"不幸的是,App Version1已停止。"
我有一个Missile.java类,一个Board.java类,它是活动,以及layout_board,我将Missle.java与布局结合起来。

Board.java:(导入所有内容)

public class Board extends AppCompatActivity {
    int coinsCounter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.board_layout);
        setCoinsCounter(0);

     //   setContentView(new MyView(this,null));
    }
}

Missile.java:

public class Missile extends ImageView {
    Paint p;

    public Missile(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        p = new Paint();
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
        p.setColor(Color.RED);
        canvas.drawBitmap(b, 0, 0, null);

    }
}

board_layout.xml:

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


<com.myfirstapplication.owner.appversion1.Missile
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/missile"/>


// Un-relevant things (TextViews, ImageViews, etc):
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/coinsCounter"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/pauseButton"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:src="@drawable/pause_pic"
    android:adjustViewBounds="true"
    android:maxHeight="50dp"
    android:maxWidth="100dp"
    android:scaleType="fitXY"
   />

 <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/coinPic"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="false"
    android:src="@drawable/coin_cartoon"
    android:adjustViewBounds="true"
    android:maxHeight="20dp"
    android:maxWidth="45dp"
    android:scaleType="fitXY"
    android:layout_toRightOf="@id/coinsCounter"
 android:layout_alignParentStart="false"
 android:layout_marginLeft="5dp"
 android:layout_marginTop="2dp" />

</RelativeLayout>

日志:

    06-16 18:29:07.931 3311-3311/com.myfirstapplication.owner.appversion1 D/AndroidRuntime: Shutting down VM
06-16 18:29:07.942 3311-3311/com.myfirstapplication.owner.appversion1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: com.myfirstapplication.owner.appversion1, PID: 3311
                                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myfirstapplication.owner.appversion1/com.myfirstapplication.owner.appversion1.Board}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.myfirstapplication.owner.appversion1.Missile
                                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                         Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.myfirstapplication.owner.appversion1.Missile
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                                            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                                            at com.myfirstapplication.owner.appversion1.Board.onCreate(Board.java:18)
                                                                                            at android.app.Activity.performCreate(Activity.java:6237)
                                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                         Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.myfirstapplication.owner.appversion1.Missile
                                                                                            at android.view.LayoutInflater.createView(LayoutInflater.java:628)
                                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
                                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                                            at com.myfirstapplication.owner.appversion1.Board.onCreate(Board.java:18) 
                                                                                            at android.app.Activity.performCreate(Activity.java:6237) 
                                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                         Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
                                                                                            at java.lang.Class.getConstructor(Class.java:528)
                                                                                            at java.lang.Class.getConstructor(Class.java:492)
                                                                                            at android.view.LayoutInflater.createView(LayoutInflater.java:592)
                                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
                                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                                            at com.myfirstapplication.owner.appversion1.Board.onCreate(Board.java:18) 
                                                                                            at android.app.Activity.performCreate(Activity.java:6237) 
                                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

它在这里说

   Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

你的Missile类需要有一个与给定签名匹配的构造函数

public class Missile extends ImageView {
    // ...

    public Missile(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

我个人推荐以下模式

public class Missile
        extends ImageView {
    public Missile(Context context) {
        super(context);
        init(context);
    }

    public Missile(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public Missile(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(21)
    public Missile(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    //  ...