我想在屏幕上看到一个按钮和一个圆圈(用画布绘图)。但是我不能这样做,特别是在XML文件中看得很好但是当我将APK发送到我的手机时出现这个错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.mehmet.catchtheball / com.example.mehmet.catchtheball.Customer}:android.view.InflateException:二进制XML文件行#0:二进制XML文件第0行:输入类com.example.mehmet.catchtheball.MyView
时出错我的activity_customer.xml
显示屏幕截图:
enter image description here
(很快,我也想在手机上显示这一点。)
MyView.java file:
package com.example.mehmet.catchtheball;
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);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("lightGray"));
canvas.drawCircle(500, 500, 150, paint);
}
}
Customer.java
档案:
package com.example.mehmet.catchtheball;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class Customer extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
MyView a = new MyView(this); //create a circle
a.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});
}
}
activity_customer.xml
档案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mehmet.catchtheball.main">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/button" />
<RelativeLayout
android:id="@+id/relativeLayoutt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true">
</RelativeLayout>
<com.example.mehmet.catchtheball.MyView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
答案 0 :(得分:1)
您必须在MyView类中实现以下构造函数
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
您的课程应如下所示: -
public class MyView extends View {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("lightGray"));
canvas.drawCircle(500, 500, 150, paint);
}
}