从ARCore应用(HelloAR)向视图绘制坐标(x,y)。膨胀类MyView时出错

时间:2018-08-31 13:50:20

标签: android view glsurfaceview arcore

我正在尝试从ARCore应用(HelloAR)向视图绘制坐标(x,y)。 我不想直接在ARCore示例中提供的GLSurfaceView上绘制。

因此,我尝试使用此网站上提到的第一种方法:

Layered SurfaceViews in a FrameLayout in Android

可悲的是,我得到了这个运行时错误:

Binary XML file line #47: Error inflating class 
com.google.ar.core.examples.java.helloar.MyView

这是我的视图类(互联网示例):

package com.google.ar.core.examples.java.helloar;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class MyView extends View {

    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = getWidth();
        int y = getHeight();
        int radius;
        radius = 100;
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        canvas.drawPaint(paint);

        // Use Color.parseColor to define HTML colors
        paint.setColor(Color.parseColor("#CD5C5C"));
        canvas.drawCircle(x / 2, y / 2, radius, paint);
    }
}

这是XML:

  <android.opengl.GLSurfaceView
  android:id="@+id/surfaceview"
  android:layout_width="fill_parent"
  android:layout_height="460dp"
  android:layout_gravity="top"
  android:visibility="visible"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintHorizontal_bias="0.0"
  app:layout_constraintStart_toStartOf="parent" />


 <com.google.ar.core.examples.java.helloar.MyView
  android:id="@+id/view"
  android:layout_width="0dp"
  android:layout_height="0dp"
  android:layout_marginBottom="8dp"
  android:layout_marginEnd="8dp"
  android:layout_marginStart="16dp"
  android:visibility="gone"
  app:layout_constraintBottom_toTopOf="@+id/surfaceview"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintHorizontal_bias="1.0"
  app:layout_constraintStart_toEndOf="@+id/messageView"
  app:layout_constraintTop_toTopOf="parent"
  app:layout_constraintVertical_bias="1.0"/>
.
.

(编辑)错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.google.ar.core.examples.java.helloar, PID: 1674
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.ar.core.examples.java.helloar/com.google.ar.core.examples.java.helloar.HelloArActivity}: android.view.InflateException: Binary XML file line #48: Binary XML file line #48: Error inflating class com.google.ar.core.examples.java.helloar.MyView
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
               Caused by: android.view.InflateException: Binary XML file line #48: Binary XML file line #48: Error inflating class com.google.ar.core.examples.java.helloar.MyView
               Caused by: android.view.InflateException: Binary XML file line #48: Error inflating class com.google.ar.core.examples.java.helloar.MyView
               Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
                  at java.lang.Class.getConstructor0(Class.java:2320)
                  at java.lang.Class.getConstructor(Class.java:1725)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:615)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                  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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                  at com.google.ar.core.examples.java.helloar.HelloArActivity.onCreate(HelloArActivity.java:140)
                  at android.app.Activity.performCreate(Activity.java:7009)
                  at android.app.Activity.performCreate(Activity.java:7000)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

我希望有人能帮助我。 解决方法也很好。

最好的问候

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:缺少AttributeSet参数。

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