Reputation: 157
I'm getting following error when I try to run my app:
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
My custom class draws a 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
}
The 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" />
I also wanted to ask if it is possible to scale what is drawn in "DrawGraphTest" class so that it fits within the bounds of the custom view on the activity?
Currently it is set up to fit the entire screen of whatever device the app is being run on (see point1), Cheers!
Upvotes: 0
Views: 147
Reputation: 18677
You are missing one of the constructors which is used to inflate layouts via 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
}
}
Upvotes: 1