2

I trying to use SurfaceView with Camera. However, findViewById(R.id.mySurfaceView) returns null. The funny thing is, I used this exact same code in the MainActivity, which worked fine. Only when I put it into another activity did it get an error (I copied all the xmls and stuff).

My main activity is only this:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(this, CammmActivity.class);
    startActivity(intent);
}
}

Here's the CameraActivity

public class CammmActivity extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

      getWindow().setFormat(PixelFormat.UNKNOWN);
      surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
      surfaceHolder = surfaceView.getHolder();
      surfaceHolder.addCallback(this);
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
 int height) {
// TODO Auto-generated method stub
if(previewing){
 camera.stopPreview();
 previewing = false;
}

if (camera != null){
 try {
  camera.setPreviewDisplay(surfaceHolder);
  camera.startPreview();
  previewing = true;
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}

The xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CammmActivity" >

<SurfaceView 
android:id="@+id/camerapreview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

</RelativeLayout>
  • 1
    you start an activity in the oncreate of an activity? that is a waste of time and memory! – WarrenFaith Jun 04 '13 at 20:10
  • @WarrenFaith I know, but see my third sentence: "The funny thing is, I used this exact same code in the MainActivity, which worked fine. Only when I put it into another activity did it give an error (I copied all the xmls and stuff)." –  Jun 04 '13 at 20:13

1 Answers1

0

I tested your code it works fine on my device. Try refreshing and cleaning your project Project -> Clean.

Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50
  • I am currently using Eclipse's emulator, but I don't think it is causing the error. The code worked on the emulator before (as I've said above, when it was in the MainActivity and not a linked activity). –  Jun 04 '13 at 20:16
  • It is not about the emulator, I don't know how much experience you have with Android but you need to Clean your project often. It gets rid of compiled classes and pushes them to compile again. Try cleaning it, I hope it will work because there is nothing wrong with your code. – Gokhan Arik Jun 04 '13 at 20:20
  • When I cleaned the project it said: 'Building workspace' has encounted a problem. Errors occurred during the build. Errors running builder 'Android Resource Manager' on project 'Preview'. java.lang.NullPointerException –  Jun 04 '13 at 20:22
  • After I clicked Ok on that popup, everything appeared fine and ran, although it still crashed. –  Jun 04 '13 at 20:23
  • It seems like different problem, try this [Eclipse Upgrade Not Working](http://stackoverflow.com/questions/9617830/eclipse-upgrade-not-working) – Gokhan Arik Jun 04 '13 at 20:28