Reputation: 649
I have a fragment
that captures 4 images, i want to pass them an activity
, But I keep getting null object. I cant figure out what to do.
This is my fragment:
public class RegFragFour extends Fragment {
Uri imageCaptureUri;
ImageView imgCbOne, imgCbTwo, imgCbThree, imgCbFour;
Button btnCbChooseOne, btnCbChooseTwo, btnCbChooseThree, btnCbChooseFour, btnCbContinueToFour;
static final int PICK_FROM_CAMERA = 1;
static final int PICK_FROM_FILE = 2;
int pressedButton = 0;
String passedPathOne, passedPathTwo, passedPathThree, passedPathFour;
onDataPassFour dataPasser;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.reg_four, container, false);
imgCbOne = (ImageView) view.findViewById(R.id.imgOne);
imgCbTwo = (ImageView) view.findViewById(R.id.imgTwo);
imgCbThree = (ImageView) view.findViewById(R.id.imgThree);
imgCbFour = (ImageView) view.findViewById(R.id.imgFour);
btnCbChooseOne = (Button) view.findViewById(R.id.btnChooseOne);
btnCbChooseTwo = (Button) view.findViewById(R.id.btnChooseTwo);
btnCbChooseThree = (Button) view.findViewById(R.id.btnChooseThree);
btnCbChooseFour = (Button) view.findViewById(R.id.btnChooseFour);
btnCbContinueToFour = (Button) view.findViewById(R.id.btnContinueToFour);
btnCbChooseTwo.setEnabled(false);
btnCbChooseThree.setEnabled(false);
btnCbChooseFour.setEnabled(false);
final String[] items = new String[]{"From Camera", "From Phone Memory"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select Image");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "tmp_avatar" + String.valueOf(System.currentTimeMillis()) + ".jpg");
imageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCaptureUri);
intent.putExtra("return data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (Exception ex) {
ex.printStackTrace();
}
dialog.cancel();
} else {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
});
final AlertDialog dialog = builder.create();
btnCbChooseOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pressedButton = 1;
dialog.show();
}
});
btnCbChooseTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pressedButton = 2;
dialog.show();
}
});
btnCbChooseThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pressedButton = 3;
dialog.show();
}
});
btnCbChooseFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pressedButton = 4;
dialog.show();
}
});
btnCbContinueToFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent Login = new Intent(getActivity(), FakeWelcome.class);
startActivity(Login);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK)
return;
Bitmap bitmap = null;
String path = "";
if (requestCode == PICK_FROM_FILE) {
imageCaptureUri = data.getData();
path = getRealPathFromURI(imageCaptureUri);
if (path == null)
path = imageCaptureUri.getPath();
if (path != null)
bitmap = BitmapFactory.decodeFile(path);
} else {
path = imageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}
switch (pressedButton){
case 1:
imgCbOne.setImageBitmap(bitmap);
btnCbChooseTwo.setEnabled(true);
passedPathOne = imageCaptureUri.getPath();;
break;
case 2:
imgCbTwo.setImageBitmap(bitmap);
btnCbChooseThree.setEnabled(true);
passedPathTwo = imageCaptureUri.getPath();;
break;
case 3:
imgCbThree.setImageBitmap(bitmap);
btnCbChooseFour.setEnabled(true);
passedPathThree = imageCaptureUri.getPath();;
break;
case 4:
imgCbFour.setImageBitmap(bitmap);
passedPathFour = imageCaptureUri.getPath();;
break;
}
dataPasser.dataInfoRegFour(passedPathOne, passedPathTwo, passedPathThree, passedPathFour);
}
public String getRealPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
dataPasser = (onDataPassFour) context;
} catch (Exception ex) {
ex.printStackTrace();
}
}
public interface onDataPassFour {
void dataInfoRegFour(String imgPathOne, String imgPathTwo, String imgPathThree, String imgPathFour);
}
}
And this is my activity:
public class FakeWelcome extends AppCompatActivity implements RegFragFour.onDataPassFour {
ImageView imgCbOne, imgCbTwo, imgCbThree, imgCbFour;
Button btnCbChooseOne, btnChooseTwo, btnChooseThree, btnChooseFour;
String _imgPathOne, _imgPathTwo, _imgPathThree, _imgPathFour;
Bitmap bitmap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fake_welcome);
imgCbOne = (ImageView) findViewById(R.id.imgOne);
imgCbTwo = (ImageView) findViewById(R.id.imgTwo);
imgCbThree = (ImageView) findViewById(R.id.imgThree);
imgCbFour = (ImageView) findViewById(R.id.imgFour);
btnCbChooseOne = (Button) findViewById(R.id.btnChooseOne);
btnChooseTwo = (Button) findViewById(R.id.btnChooseTwo);
btnChooseThree = (Button) findViewById(R.id.btnChooseThree);
btnChooseFour = (Button) findViewById(R.id.btnChooseFour);
btnCbChooseOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bitmap = BitmapFactory.decodeFile(_imgPathOne);
imgCbOne.setImageBitmap(bitmap);
}
});
btnChooseTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bitmap = BitmapFactory.decodeFile(_imgPathTwo);
imgCbTwo.setImageBitmap(bitmap);
}
});
btnChooseThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bitmap = BitmapFactory.decodeFile(_imgPathThree);
imgCbThree.setImageBitmap(bitmap);
}
});
btnChooseFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bitmap = BitmapFactory.decodeFile(_imgPathFour);
imgCbFour.setImageBitmap(bitmap);
}
});
}
@Override
public void dataInfoRegFour(String imgPathOne, String imgPathTwo, String imgPathThree, String imgPathFour) {
_imgPathOne = imgPathOne;
_imgPathTwo = imgPathTwo;
_imgPathThree = imgPathThree;
_imgPathFour = imgPathFour;
}
}
This is my error:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.shaka.myparselogin.RegistrationFragments.RegFragFour$onDataPassFour.dataInfoRegFour(android.graphics.Bitmap, android.graphics.Bitmap, android.graphics.Bitmap, android.graphics.Bitmap)' on a null object reference
at com.example.shaka.myparselogin.RegistrationFragments.RegFragFour.onActivityResult(RegFragFour.java:168)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
it points to this line:
dataPasser.dataInfoRegFour(passedPathOne, passedPathTwo, passedPathThree, passedPathFour);
any ideas?
Upvotes: 1
Views: 2062
Reputation: 3235
Well, obviously the dataPasser
object is null. In the code snippets shown it's only given a value in the Fragments
's onAttach()
method. So you could place a breakpoint in onAttach()
, debug your application and confirm that it's actually never called.
As for why it's not called: You seem to be using some version of an AppCompat library. There are e.g. these two discussions on StackOverflow covering the fact that the onAttach()
method signature has changed from onAttach(Activity)
to onAttach(Context)
:
onAttach() not called in Fragment
Android Fragment onAttach() deprecated
So probably the problem is that you have implemented the one that takes a Context
as a parameter and your Activity
tries to call the one that takes an Activity
as a parameter.
You could check out e.g. those two links and probably you'll just need to change your onAttach()
method signature accordingly.
The code change:
From...
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
dataPasser = (onDataPassFour) context;
} catch (Exception ex) {
ex.printStackTrace();
}
}
...to...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
dataPasser = (onDataPassFour) activity;
} catch (Exception ex) {
ex.printStackTrace();
}
}
Upvotes: 2