The question is: there is draw_activity

<?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" android:background="#FFF" android:orientation="vertical"> <com.example.android.threepointscircle.DrawActivity.DrawView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/drawView"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:id="@+id/backButton" android:text="back" /> </RelativeLayout> 

Have DrawActivity

 public class DrawActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new DrawView(this)); Button backButton = (Button) findViewById(R.id.backButton); backButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View view) { /** Π˜Π½Ρ‚Π΅Π½Ρ‚ для ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄Π° Π½Π° экран Π²Π²ΠΎΠ΄Π° Ρ‚ΠΎΡ‡Π΅ΠΊ */ Intent drawActivity = new Intent(DrawActivity.this, MainActivity.class); startActivity(drawActivity); } }); } 

In DrawView circle at a given point. Under it, I want to make a return button to activate, where the coordinates of points are initially entered. It is impossible neither to see the button, nor to initialize it.

What am I doing wrong, can someone tell me how best to create a button on a Canvas ?

DrawView class with a constructor (methods and onDraw do not show, they work, checked)

 //ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ класса DrawActivity public int width; public int height; public float cx; public float cy; public float[] coordinates; class DrawView extends View { /** Π—Π°Π΄Π°Π΅ΠΌ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ класса * Ρ€ - ΠΊΠΈΡΡ‚ΡŒ для Ρ„ΠΈΠ³ΡƒΡ€ * Ρ€Π’ - ΠΊΠΈΡΡ‚ΡŒ для тСкста * rectF - Ρ€Π°ΠΌΠΊΠ° для подписСй Ρ‚ΠΎΡ‡Π΅ΠΊ * radius - радиус окруТности * tk - коэффициСнт ΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΡ Π΄Π»ΠΈΠ½Ρ‹ ΠΏΠΎΠ»ΠΎΠ²ΠΈΠ½Ρ‹ экрана ΠΊ радиусу окруТности */ Paint p; Paint pT; Rect rect; RectF rectF; float radius, tk; public DrawView(Context context) { super(context); Intent intent = getIntent(); p = new Paint(); pT = new Paint(); rectF = new RectF(); //ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΡˆΠΈΡ€ΠΈΠ½Ρ‹ ΠΈ высоты экрана initWidthAndHeight(context); cx = intent.getFloatExtra("cx", 0); cy = intent.getFloatExtra("cy", 0); coordinates = intent.getFloatArrayExtra("coord"); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ радиус окруТности getDistance(); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ коэффициСнт ΡΠΎΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΡ ΡˆΠΈΡ€ΠΈΠ½Ρ‹ экрана ΠΊ радиусу окруТности tk = (float) 0.8 * ((width / 2) / radius); } 

1 answer 1

You have only DrawView on the screen.

 setContentView(new DrawView(this)); 

In this method you need to pass the Layout you want to display.
Like this:

 setContentView(R.layout.draw_activity); 

UPD 09/12/2016

1) Check that you have the necessary modifiers in front of the DrawView class.

public static class DrawView

Because this is your inner class DrawActivity.

2) Do not forget to add the necessary constructors, namely:

 public DrawView(Context context) { super(context); } public DrawView(Context context, AttributeSet attrs) { super(context, attrs); } public DrawView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } 

UPD 09/15/2016 How to access getIntent from static DrawView
To access getIntent write the following to the constructor:

 public DrawView(Context context) { super(context); Intent intent = ((Activity)context)getIntent(); .... } 
  • When I call Layout, as you showed, you get the error java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.android.threepointscircle / com.example.android.threepointscircle.DrawActivity}: android.view.InflateException: Binary XML file line # 8: Error inflating class com.example.android.threepointscircle.DrawActivity.DrawView DrawView as shown by me is working fine - Velsevool
  • 1) Do you have public static modifiers in front of the DrawView class? 2) Does the DrawView class have all the necessary constructors? Namely: <code> public DrawView (Context context) {super (context); } public DrawView (Context context, AttributeSet attrs) {super (context, attrs); } public DrawView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); } </ code> - Egor tepikin
  • Described in more detail in the answer to the question (see UPD 09/12/2016). PS It would be nice to see the full code of DrawActivity. - Egor tepikin
  • In the question added the designer of my DrawView. If I put him a static modifier, then I can not use Intent. - Velsevool
  • The phrase "can not use Intent" most likely meant that you need a link to the DrawActivity instance. If so, then it is easy to get it as follows: (DrawActivity) this.getContext (). If the phrase meant something else then describe in more detail - what is the problem. - Egor tepikin