簡體   English   中英

StartActivityForResult上的java.lang.NullPointerException

[英]java.lang.NullPointerException on StartActivityForResult

我是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());
}

現在我想知道為什么會有空指針異常? 在與Camera連接的startActivityForResult和與Gallery連接的startActivityForResult中

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" />

您的其他活動(圖庫和相機)將需要一個沒有活動創建將失敗的布局的布局。

我建議您沒有Gallery和Camera擴展活動類,而是可以發送父活動的上下文並使用它來啟動活動

更新:這是解決方案,希望對您有所幫助

主要活動

@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);
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM