简体   繁体   中英

Why am I getting a nullpointerexception on .setOnTouchListener()?

I'm trying to add an onTouchListener, to an ImageView so I can get the values of a pixel that a user chooses from an image they take. Therefore, I implemented onTouchListener in my main activity and used it as the onTouchListener for ImageView imageview.

My code is as follows:

package com.andrew.omnidropper;

import java.io.File;
import java.net.URI;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class OmniDropperActivity extends Activity implements OnTouchListener {
protected static final int TAKE_PICTURE_INTENT = 0;
protected static final int SELECT_PREVIOUS_PHOTO_INTENT = 1;
/** Called when the activity is first created. */

public Button cameraButton;

public ImageView imageview;
public ImageView colordisplay;

private Bitmap bitmap;

private Uri imageUri;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    cameraButton = (Button) findViewById(R.id.camerabutton);
    imageview = (ImageView) findViewById(R.id.imageview);
    colordisplay = (ImageView) findViewById(R.id.colordisplay);

    cameraButton.setOnClickListener(clickListener);
    imageview.setOnTouchListener(this);
}
OnClickListener clickListener = new OnClickListener() {
    public void onClick(View v) {
        if(v.getId() == R.id.camerabutton){
            Intent intent = new    Intent("android.media.action.IMAGE_CAPTURE");
            File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photo));
            imageUri = Uri.fromFile(photo);
            startActivityForResult(intent, 0);
            setContentView(R.layout.selectcolorspot);
        }
        /*if(v.getId() == R.id.imagebutton){
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"),
                    SELECT_PREVIOUS_PHOTO_INTENT);
            setContentView(R.layout.selectcolorspot);
        }(*/
        /*if(v.getId() == R.id.scratchbutton){
            setContentView(R.layout.makenewcolor);
        }*/
    }
};
private int colorPicked;

public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 0:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);
                imageview = (ImageView) findViewById(R.id.imageview);
                ContentResolver cr = getContentResolver();
                try {
                    bitmap = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, selectedImage);

                    imageview.setImageBitmap(bitmap);
                    imageview.setScaleType(ImageView.ScaleType.FIT_XY);
                    Toast.makeText(this, "Select an point on the image and press OK",
                                   Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                    Log.e("Camera", e.toString());
                }
            }
    }
}

public boolean onTouch(View v, MotionEvent e) {
    if(v.getId() == R.id.imageview){
        if(e.getAction() == MotionEvent.ACTION_DOWN && bitmap != null){
            colorPicked = bitmap.getPixel((int) e.getX(),(int) e.getY());
            setContentView(R.layout.iscolorok);
            colordisplay.setBackgroundColor(colorPicked);
        }
    }
    return false;
}
}

but when I try to run it, it crashes.

Here's the logcat output:

03-28 20:43:54.613: D/AndroidRuntime(4761): Shutting down VM
03-28 20:43:54.613: W/dalvikvm(4761): threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-28 20:43:54.621: E/AndroidRuntime(4761): FATAL EXCEPTION: main
03-28 20:43:54.621: E/AndroidRuntime(4761): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andrew.omnidropper/com.andrew.omnidropper.OmniDropperActivity}: java.lang.NullPointerException
03-28 20:43:54.621: E/AndroidRuntime(4761):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at android.app.ActivityThread.access$1500(ActivityThread.java:124)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at android.os.Looper.loop(Looper.java:123)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at android.app.ActivityThread.main(ActivityThread.java:3806)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at java.lang.reflect.Method.invoke(Method.java:507)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at dalvik.system.NativeStart.main(Native Method)
03-28 20:43:54.621: E/AndroidRuntime(4761): Caused by: java.lang.NullPointerException
03-28 20:43:54.621: E/AndroidRuntime(4761):     at com.andrew.omnidropper.OmniDropperActivity.onCreate(OmniDropperActivity.java:47)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-28 20:43:54.621: E/AndroidRuntime(4761):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
03-28 20:43:54.621: E/AndroidRuntime(4761):     ... 11 more
03-28 20:43:59.519: I/Process(4761): Sending signal. PID: 4761 SIG: 9

I don't think imageview is null because I declare it, so why would it throw a nullPointerException?

Your imageview is probably null. The findViewById method will return null if it can't find the view. Are you sure the ImageView you're trying to fetch is defined in your main layout? I would step through it with a debugger and see if imageview is null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM