我有一个fragment
可以捕获4个图像,我想传递它们activity
,但我一直得到null对象。我无法弄清楚该怎么做。
这是我的片段:
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);
}
}
这是我的活动:
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;
}
}
这是我的错误:
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)
它指向这一行:
dataPasser.dataInfoRegFour(passedPathOne, passedPathTwo, passedPathThree, passedPathFour);
任何想法?
答案 0 :(得分:2)
嗯,显然dataPasser
对象为空。在显示的代码段中,它仅在Fragments
onAttach()
方法中给出了值。因此,您可以在onAttach()
中放置一个断点,调试您的应用程序并确认它实际上从未被调用过。
至于为什么没有调用它:你好像在使用某个版本的AppCompat库。例如有这两个关于StackOverflow的讨论涵盖了onAttach()
方法签名已从onAttach(Activity)
更改为onAttach(Context)
的事实:
onAttach() not called in Fragment
Android Fragment onAttach() deprecated
所以可能问题是你已经实现了以Context
作为参数的那个,而Activity
尝试调用以Activity
作为参数的那个。{ / p>
你可以看看,例如这两个链接,您可能只需要相应地更改onAttach()
方法签名。
代码更改:
...从
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
dataPasser = (onDataPassFour) context;
} catch (Exception ex) {
ex.printStackTrace();
}
}
...到...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
dataPasser = (onDataPassFour) activity;
} catch (Exception ex) {
ex.printStackTrace();
}
}