StartActivityForResult上的java.lang.NullPointerException

时间:2014-06-03 23:51:57

标签: java android camera gallery

我是Android编程的新手,我正在尝试实现具有两个按钮的应用程序,用于打开相机并制作照片以及打开图库和选择照片。

我知道如何在设计师模式下使用onClick()来做到这一点,但问题是,我不想把所有东西放在一个大的班级里。所以我做了两个不同的课程来处理相机和画廊的选择。

这是我的代码:

public class Camera extends Activity implements Button.OnClickListener {

   private static final int CAMERA_REQUEST = 1888;

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
           Bundle extras = data.getExtras();
           Bitmap original = (Bitmap) extras.get("data");
           ImageView iv = (ImageView) findViewById(R.id.oko);
           iv.setImageBitmap(original);
       }
   }

   @Override
   public void onClick(View view) {
      Intent cameraIntent = new     Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(cameraIntent, CAMERA_REQUEST);
   }
}

图库类:

public class Gallery extends Activity implements Button.OnClickListener {

    @Override
    public void onActivityResult(int requestCode , int resultCode, Intent data) {
        if(resultCode  == RESULT_OK) {
            if(requestCode ==1) {
                ImageView oko = (ImageView)findViewById(R.id.oko);
                oko.setImageURI(data.getData()); // pobranie z galerii
                Bitmap okobit = ((BitmapDrawable) oko.getDrawable()).getBitmap();
                ImageView iv = (ImageView) findViewById(R.id.oko);
                iv.setImageBitmap(okobit);
            }
        }
    }

    @Override
    public void onClick(View view) {
        Intent gallery = new Intent();
        gallery.setType("image/*");
        gallery.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(gallery, "Select Image"), 1); // here is a NULL exception
    }
}

以及使用这些类的方法:

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

    Button gallery  = (Button)findViewById(R.id.gallery); 
    gallery.setOnClickListener(new Gallery());

    Button camera = (Button)findViewById(R.id.camera); 
    camera.setOnClickListener(new Camera());
}

现在我想知道为什么我有空指针异常?两者都在startActivityForResult连接Camera和startActivityForResult连接Gallery

java.lang.NullPointerException
        at android.app.Activity.startActivityForResult(Activity.java:3390)
        at android.app.Activity.startActivityForResult(Activity.java:3351)
        at com.example.teczowka.app.Gallery.onClick(Gallery.java:39)

我的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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.teczowka.app.MainActivity"
android:background="#dbff56"
android:name="android.hardware.camera"
android:required="true">

<ImageView
    android:layout_width="550dp"
    android:layout_height="350dp"
    android:id="@+id/oko"
    android:visibility="visible" />

<Button
    android:layout_width="110dp"
    android:layout_height="50dp"
    android:text="Recognize"
    android:id="@+id/recognize"
    android:layout_alignParentBottom="true" />

<Button
    android:layout_width="110dp"
    android:layout_height="50dp"
    android:text="Gallery"
    android:id="@+id/gallery"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="110dp"
    android:layout_height="50dp"
    android:text="Camera"
    android:id="@+id/camera"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:longClickable="true"
    android:onClick="onClick" />

1 个答案:

答案 0 :(得分:1)

您的其他活动(图库和相机)需要没有布局的布局,活动创建将失败。

我建议你没有Gallery和Camera扩展Activity类,而是你可以发送父活动的上下文并用它来启动活动

更新:这是解决方案,希望它有所帮助

主要活动

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

    Button gallery = (Button) findViewById(R.id.gallery);
    gallery.setOnClickListener(new Gallery(this));

    Button camera = (Button) findViewById(R.id.camera);
    camera.setOnClickListener(new Camera(this));

}

@Override
public void onActivityResult(int requestCode , int resultCode, Intent data) {
    if(resultCode  == RESULT_OK) {
        if(requestCode ==1) {
            ImageView oko = (ImageView)findViewById(R.id.oko);
            oko.setImageURI(data.getData()); // pobranie z galerii
            Bitmap okobit = ((BitmapDrawable) oko.getDrawable()).getBitmap();
            ImageView iv = (ImageView) findViewById(R.id.oko);
            iv.setImageBitmap(okobit);
        }
    }
}

图库类

public class Gallery implements View.OnClickListener {

private Activity mContext;

public Gallery(Activity activity){
    this.mContext = activity;
}

@Override
public void onClick(View view) {
     Intent gallery = new Intent();
        gallery.setType("image/*");
        gallery.setAction(Intent.ACTION_GET_CONTENT);
        mContext.startActivityForResult(Intent.createChooser(gallery, "Select Image"), 1); // here is a NULL exception
    }
}

相机类

public class Camera implements View.OnClickListener {
private static final int CAMERA_REQUEST = 1888;
private Activity mContext;

public Camera(Activity activity) {
    this.mContext = activity;
}

@Override
public void onClick(View view) {
    Intent cameraIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    mContext.startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}