Android 在 activity 中查看自定义视图时应用程序崩溃
Android application crashing when viewing custom view in activity
当我尝试 运行 我的应用程序时出现以下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{g.companieshouse.companieshouse/g.companieshouse.companieshouse.GraphActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class g.companieshouse.companieshouse.DrawGraphTest
我的习惯class画了一个canvas:
public class DrawGraphTest extends View {
int mWidth = this.getResources().getDisplayMetrics().widthPixels;
int mHeight = this.getResources().getDisplayMetrics().heightPixels;
public DrawGraphTest(Context context) {
super(context);
//Various paints and such...
//Set point to middle of screen
point1 = new Point(mWidth / 2, mHeight / 2);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw various stuff to canvas
}
activity:
public class GraphActivity extends AppCompatActivity {
DrawGraphTest drawGraphTest;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
drawGraphTest = (DrawGraphTest)findViewById(R.id.drawGraphTest);
}
}
XML:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GraphActivity">
<g.companieshouse.companieshouse.DrawGraphTest
android:id="@+id/drawGraphTest"
android:layout_width="350dp"
android:layout_height="300dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
我还想问一下是否可以缩放 "DrawGraphTest" class 中绘制的内容,使其适合 activity 上自定义视图的范围?
目前它被设置为适合任何设备的整个屏幕运行(见第 1 点),干杯!
您缺少用于通过 XML 扩充布局的构造函数之一:
public class DrawGraphTest extends View {
int mWidth = this.getResources().getDisplayMetrics().widthPixels;
int mHeight = this.getResources().getDisplayMetrics().heightPixels;
// This constructor is used only if you instantiate your view dinamically (java)
public DrawGraphTest(Context context) {
super(context);
init();
}
// You are missing this constructor.. Add it so Android can instantiate your view via xml
public DrawGraphTest(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
//Various paints and such...
//Set point to middle of screen
point1 = new Point(mWidth / 2, mHeight / 2);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw various stuff to canvas
}
}
当我尝试 运行 我的应用程序时出现以下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{g.companieshouse.companieshouse/g.companieshouse.companieshouse.GraphActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class g.companieshouse.companieshouse.DrawGraphTest
我的习惯class画了一个canvas:
public class DrawGraphTest extends View {
int mWidth = this.getResources().getDisplayMetrics().widthPixels;
int mHeight = this.getResources().getDisplayMetrics().heightPixels;
public DrawGraphTest(Context context) {
super(context);
//Various paints and such...
//Set point to middle of screen
point1 = new Point(mWidth / 2, mHeight / 2);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw various stuff to canvas
}
activity:
public class GraphActivity extends AppCompatActivity {
DrawGraphTest drawGraphTest;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
drawGraphTest = (DrawGraphTest)findViewById(R.id.drawGraphTest);
}
}
XML:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GraphActivity">
<g.companieshouse.companieshouse.DrawGraphTest
android:id="@+id/drawGraphTest"
android:layout_width="350dp"
android:layout_height="300dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
我还想问一下是否可以缩放 "DrawGraphTest" class 中绘制的内容,使其适合 activity 上自定义视图的范围?
目前它被设置为适合任何设备的整个屏幕运行(见第 1 点),干杯!
您缺少用于通过 XML 扩充布局的构造函数之一:
public class DrawGraphTest extends View {
int mWidth = this.getResources().getDisplayMetrics().widthPixels;
int mHeight = this.getResources().getDisplayMetrics().heightPixels;
// This constructor is used only if you instantiate your view dinamically (java)
public DrawGraphTest(Context context) {
super(context);
init();
}
// You are missing this constructor.. Add it so Android can instantiate your view via xml
public DrawGraphTest(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
//Various paints and such...
//Set point to middle of screen
point1 = new Point(mWidth / 2, mHeight / 2);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw various stuff to canvas
}
}