我有一个按钮:
final Button dialogButtonFotoPerfil1 = (Button) dialogFotosPerfil
.findViewById(R.id.botonFotoPerfil1);
dialogButtonFotoPerfil1.setOnClickListener(new MediaGaleryActivity());
我想展示一个图像选择器。MediaGaleryActivity.java 是:
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import com.gayworld.R;
import com.gayworld.utils.Utilidades;
public class MediaGaleryActivity extends Activity implements OnClickListener {
private static final int SELECT_IMAGE = 1;
private static String TAG;
@Override
public void onClick(View v) {
TAG = "MediaGaleryActivity.onClick";
Log.d(TAG, "Inicio.");
Intent intent = null;
switch (v.getId()) {
case R.id.botonFotoPerfil1:
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Log.d(TAG, "Error valor gallery "+intent.toString());
startActivityForResult(Intent.createChooser(intent,
"Select Picture"),SELECT_IMAGE);
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
TAG = "MediaGaleryActivity.onActivityResult";
Log.d(TAG, "Inicio.");
if(resultCode==RESULT_OK && requestCode==SELECT_IMAGE){
Uri selectedImage=data.getData();
String path=getPath(selectedImage);
Bitmap bitmapImage=BitmapFactory.decodeFile(path);
ImageView image=(ImageView)findViewById(R.id.image);
image.setImageBitmap(bitmapImage);
}
}
public String getPath(Uri uri){
TAG = "MediaGaleryActivity.getPath";
Log.d(TAG, "Inicio.");
String[] filePathColumn={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
return cursor.getString(columnIndex);
}
}
但我得到了一个 NullPointerException。
日志猫:
05-25 13:52:15.287: D/MediaGaleryActivity.onClick(11620): Inicio.
05-25 13:52:15.295: D/MediaGaleryActivity.onClick(11620): Error valor gallery Intent { act=android.intent.action.GET_CONTENT typ=image/* }
05-25 13:52:15.295: D/AndroidRuntime(11620): Shutting down VM
05-25 13:52:15.295: W/dalvikvm(11620): threadid=1: thread exiting with uncaught exception (group=0x40a2c1f8)
05-25 13:52:15.334: E/AndroidRuntime(11620): FATAL EXCEPTION: main
05-25 13:52:15.334: E/AndroidRuntime(11620): java.lang.NullPointerException
05-25 13:52:15.334: E/AndroidRuntime(11620): at android.app.Activity.startActivityForResult(Activity.java:3190)
05-25 13:52:15.334: E/AndroidRuntime(11620): at com.gayworld.screen.activity.MediaGaleryActivity.onClick(MediaGaleryActivity.java:34)
05-25 13:52:15.334: E/AndroidRuntime(11620): at android.view.View.performClick(View.java:3511)
05-25 13:52:15.334: E/AndroidRuntime(11620): at android.view.View$PerformClick.run(View.java:14105)
05-25 13:52:15.334: E/AndroidRuntime(11620): at android.os.Handler.handleCallback(Handler.java:605)
05-25 13:52:15.334: E/AndroidRuntime(11620): at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 13:52:15.334: E/AndroidRuntime(11620): at android.os.Looper.loop(Looper.java:137)
05-25 13:52:15.334: E/AndroidRuntime(11620): at android.app.ActivityThread.main(ActivityThread.java:4575)
05-25 13:52:15.334: E/AndroidRuntime(11620): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 13:52:15.334: E/AndroidRuntime(11620): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 13:52:15.334: E/AndroidRuntime(11620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
05-25 13:52:15.334: E/AndroidRuntime(11620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
05-25 13:52:15.334: E/AndroidRuntime(11620): at dalvik.system.NativeStart.main(Native Method)
我收到此错误,我不知道我还能做些什么。我一直在阅读很多博客,答案,..我也寻求谷歌帮助......但没有。我总是得到同样的错误。任何想法??因为我真的很坚持。
提前致谢。