Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7631165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:17:02+00:00 2026-05-31T06:17:02+00:00

I have created an application where i have a camera preview. I am trying

  • 0

I have created an application where i have a camera preview. I am trying to create a touch event such that when i touch the preview, something will happen (let say, a toast will appear).
The problem is that when i am touching the preview, after 2 seconds the application crashes.
I have two classes:

CameraActivity:

public class CameraActivity extends Activity {
    private static final String TAG = "CameraDemo";
    Preview preview;

    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         preview = new Preview(this);
        ((FrameLayout) findViewById(R.id.preview)).addView(preview);
        ((FrameLayout) findViewById(R.id.preview)).setOnTouchListener(preview);

        Log.d(TAG, "Camera Activity Created.");
    }
}

and Preview:

class Preview extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
private static final String TAG = "Preview";

SurfaceHolder mHolder;
public Camera camera;

Preview(Context context) {
    super(context);

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

// Called once the holder is ready
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    try {
        camera.setDisplayOrientation(90);

        camera.setPreviewDisplay(holder);

        camera.setPreviewCallback(new PreviewCallback() {
            // Called for each frame previewed
            public void onPreviewFrame(byte[] data, Camera camera) {
                Log.d(TAG, "onPreviewFrame called at: " +  System.currentTimeMillis());
                Preview.this.invalidate();
            }
        });

    } catch (IOException e) {
      e.printStackTrace();
    }


}



// Called when the holder is destroyed
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
}

// Called when holder has changed
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    camera.startPreview();
}

public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(findViewById(R.id.preview).getContext(), "BEFORE SHUTTER", Toast.LENGTH_LONG).show();
    return false;
}
 }

Can someone please help and tell me what i am doing wrong?

Here is the LogCat (the part where the crash appears):

03-08 12:11:48.335: D/AndroidRuntime(1167): Shutting down VM
03-08 12:11:48.335: W/dalvikvm(1167): threadid=1: thread exiting with uncaught    exception (group=0x40015578)
03-08 12:11:48.343: E/AndroidRuntime(1167): FATAL EXCEPTION: main
03-08 12:11:48.343: E/AndroidRuntime(1167): java.lang.NullPointerException
03-08 12:11:48.343: E/AndroidRuntime(1167):     at com.marakana.Preview.onTouch(Preview.java:80)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at     android.view.View.dispatchTouchEvent(View.java:3934)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at  android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at  android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1733)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1151)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1717)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2215)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1886)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at android.os.Looper.loop(Looper.java:123)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at android.app.ActivityThread.main(ActivityThread.java:3687)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at java.lang.reflect.Method.invoke(Method.java:507)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-08 12:11:48.343: E/AndroidRuntime(1167):     at dalvik.system.NativeStart.main(Native Method)
03-08 12:11:48.378: D/dalvikvm(1167): GC_FOR_MALLOC freed 943K, 59% free 2633K/6407K, external 1596K/2108K, paused 15ms
03-08 12:11:48.531: D/dalvikvm(1167): GC_CONCURRENT freed 21K, 52% free 3084K/6407K, external 1596K/2108K, paused 2ms+26ms
03-08 12:11:56.109: I/Process(1167): Sending signal. PID: 1167 SIG: 9

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:17:03+00:00Added an answer on May 31, 2026 at 6:17 am

    You have an issue with context

    Toast.makeText(findViewById(R.id.preview).getContext(), "BEFORE SHUTTER", Toast.LENGTH_LONG).show(); 
    

    You cannot get context this way. In your constructor you pass in context:

    Preview(Context context) {
    

    I would create a private instance of context

    private Context ctx;
    

    and then in the constructor add

    this.ctx = context;
    

    and then change the toast message to

    Toast.makeText(ctx, "BEFORE SHUTTER", Toast.LENGTH_LONG).show();
    

    This will also give you context in most other places in your class too therefore making life a little easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently built an application that shows you a camera preview on screen.
I have created an application that writes some data to the root folder of
I have created an application that runs in the taskbar. When a user clicks
I have created an application that has a toolbar, menubar and content area. I
I have created an application that uses settings.settings to store some user specific settings
I have created an application (compiled any cpu) that use a lib also compiled
I have an object named Camera in my MVC 3 application. I created a
I have built an application which takes photos when you touch the preview. I
I have created an application which needs 'hand-over' to the support group in the
I have created an application in facebook,all is working fine except the publish to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.