0

Could someone help me to create SurfaceView in a fragment? Below is my code. It always stops at thr line: if (!surfaceHolder.getSurface().isValid()) and I don't know why.

Fragment code:

<pre>
public class FirstActivity extends Fragment/* implements OnTouchListener*/ {

    CameraView cameraView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        /* instantiating the surface view */
        cameraView = new CameraView(this.getActivity());
        /* setting the listener - this, because it is defined within the activity */
//      cameraView.setOnTouchListener(this);

    }

//  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//      View v = inflater.inflate(R.layout.lin, null);
//      
//      cameraView = (CameraView) v.findViewById(R.id.cameraView);
//      
//      return v;
//
//  }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return new CameraView(getActivity());
    }

//  @Override
//  public boolean onTouch(View arg0, MotionEvent arg1) {
//      // TODO Auto-generated method stub
//      return false;
//  }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onResume() {
        super.onResume();
        cameraView.onResumeCameraView();
    }

    @Override
    public void onPause() {
        super.onPause();
        cameraView.onPauseCameraView();
    }

}
</pre>

CameraView code:

<pre>
public class CameraView extends SurfaceView implements Runnable {
    Thread thread = null;

    SurfaceHolder surfaceHolder;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Bitmap bitmap;

    int WIDTH = 320;
    int HEIGHT = 240;

    volatile boolean running = false;


    public CameraView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        surfaceHolder = getHolder();
        bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888/*Bitmap.Config.ALPHA_8*//*Bitmap.Config.RGB_565*/);
        Log.d("S3", "stworzono bitmape");
    }

    public void onResumeCameraView() {
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public void onPauseCameraView() {
        boolean retry = true;
        running = false;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (running) {   

            if (!surfaceHolder.getSurface().isValid()) {
                Log.d("S3", "blad");
                continue;
            }

            Log.d("S3", "dalej");
            Canvas canvas = surfaceHolder.lockCanvas();

            canvas.drawColor(Color.WHITE);
            paint.setColor(Color.RED);
            canvas.drawRect(0, 0, 100, 100, paint);
            surfaceHolder.unlockCanvasAndPost(canvas);

        }
    }
}
</pre>

Thank You for help.

  • 2
    If by "stops", you mean that your app crashes, use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Sep 19 '15 at 22:18
  • No, by stops, I mean that surfaceHolder.getSurface().isValid() returns always false. So it stops on this 'if'. – user5354725 Sep 19 '15 at 22:57

3 Answers3

1

Use SurfaceHolder.addCallback() to register a callback interface. It will notify you when the surface to available, destroyed and when it changes.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
0

I don't see any code in your post that either references a SurfaceView declared in XML or adds the SurfaceView programmatically to the layout. You need to do either of those, or the SurfaceView can't be drawn.

To add progammatically, in onCreateView(...), do:

mCameraView = new CameraView(...);
ViewGroup myRootLayout = (ViewGroup)findViewById(...)
myRootLayout.addView(mCameraView);
Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71
  • Thank You for answer. Could You tell me what shoul I add because I'm a beginner in java and android. – user5354725 Sep 20 '15 at 09:01
  • I added some more explanation. SurfaceView is a View. It needs to be added to the XML like all other Views. Declare it in XML or do as I show in answer and add it programmatically. There might be other bugs because it's your own custom class extending the framework's SurfaceView, but that's the general idea. If you're having continued issues, simplify the problem and learn how to add a basic SurfaceView to a layout, then extend it into your own class and add it the same way. – Gunnar Karlsson Sep 20 '15 at 09:01
  • Could You tell me what is wrong with this code I wrote? I am still trying but it doesn't work:/ What should be in XML file? – user5354725 Sep 21 '15 at 08:27
  • I changed onCreateView and show it above. Could You look at this. Thank You. – user5354725 Sep 21 '15 at 09:04
0

My onCreateView function looks:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        RelativeLayout relativeLayout = new RelativeLayout(this.getActivity());
        TextView textView = new TextView(this.getActivity());
        textView.setText("Simply dummy text");

        relativeLayout.addView(textView);
        relativeLayout.addView(cameraView);

        return relativeLayout;
}

I can create and add textView without any problems. But if I'm trying to add cameraView the application crashes. Why? I'm creating cameraView in function onActivityCreated