Note that there are some explanatory texts on larger screens.

plurals
  1. POLinking nested SurfaceView class inside xml
    primarykey
    data
    text
    <p>I have a class inside a class that extends SurfaceView like this:</p> <p><strong>MainActivity.java</strong></p> <pre><code>public class MainActivity extends Activity implements OnTouchListener { SurfaceTest mySurface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.surface); mySurface = (SurfaceTest) findViewById(R.layout.surface); mySurface.setOnTouchListener(this); } @Override public boolean onTouch(View arg0, MotionEvent arg1) { // TODO Auto-generated method stub // ... some code ... return true; } public class SurfaceTest extends SurfaceView implements Runnable { SurfaceHolder myHolder; public SurfaceTest(Context context) { super(context); // TODO Auto-generated constructor stub myHolder = getHolder(); } public SurfaceTest(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub myHolder = getHolder(); } public SurfaceTest(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub myHolder = getHolder(); } @Override public void run() { // TODO Auto-generated method stub while (true) { if (!myHolder.getSurface().isValid()) { continue; } Canvas canvas = myHolder.lockCanvas(); //...some code... myHolder.unlockCanvasAndPost(canvas); } } } } </code></pre> <p><strong>surface.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;com.example.testsurfaceview.MainActivity.SurfaceTest android:id="@+id/surfaceView" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /&gt; &lt;/FrameLayout&gt; </code></pre> <p>I have an error in Graphical Layout of this xml file saying </p> <pre><code>"The following classes could not be instantiated: - com.example.testsurfaceview.MainActivity.SurfaceTest" </code></pre> <p>One possible solution that worked for me was to create new class for <code>SurfaceTest</code> class and then using <code>com.example.testsurfaceview.SurfaceTest</code> in xml file.</p> <p>But the problem again is I use a lot of information from <code>onTouch</code> event which is used to draw on canvas in <code>run()</code> method. This is the reason I implemented <code>SurfaceTest</code> class inside <code>MainActivity</code></p> <p>So I have two choices right now:</p> <p>1) make xml work for nested SurfaceTest class 2) create new SurfaceTest class and pass all the info obtained in <code>onTouch</code> using an Intent.</p> <p>What do you suggest?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload