我遇到了与nullpoint异常崩溃的问题。 我知道它在尝试从pictureTalkFragment获取ArrayList时崩溃了。在这个类中只设置为PictureTalkFragment ptf; 换句话说,我试图从一个类而不是该类的实例中获取一个元素(在ptf中为arraylist设置getter / setter,并使arraylist公开作为替代)。
但我只是想让菜鸟弄清楚如何正确处理类之间的实例(活动--->片段和返回等)。在Java中,我通常只在构造函数中有一个referance,它通过创建新类来发送实例/ referance。但是在Android中所有这些onCreate(getActivity,getContext ++),我很困惑:P何时用户在哪里以及如何:(
EditPicture是从GridViewAdapter中的此代码启动的,该代码从PictureTalkFragment扩展(在onlongclicklistener中编辑)
row.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v) {
PopupMenu popMenu = new PopupMenu(v.getContext(), v);
popMenu.getMenuInflater().inflate(R.menu.picturetalk_popup_menu, popMenu.getMenu());
popMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.edit:
Intent intent = new Intent(getContext(), EditPicture.class);
intent.putExtra("itemUUID", item.getId());
String s = new String("");
context.startActivity(intent);
break;
case R.id.remove:
FileInteraction fileInteraction = new FileInteraction();
fileInteraction.deleteFilesAndFolder(item.getImagePath());
item.setTitle("");
notifyDataSetChanged();
break;
default:
//
}
return true;
}
});
popMenu.show();
return true;
}
});
return row;
EditPicture类
public class EditPicture extends Activity {
private EditText text;
private Button applyBtn;
private ArrayList<PictureItem> piArray;
private PictureItem pi;
private UUID itemID;
private PictureTalkFragment ptf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
itemID = (UUID) getIntent().getSerializableExtra("itemUUID");
SetLocalArray(ptf.getArray()); //Nullpoint here, and i know why. But not how to get the allready created instance of this class
getPictureItem();
setContentView(R.layout.picturetalk_edit_pic);
text = (EditText) findViewById(R.id.editName);
text.setText(pi.getTitle());
applyBtn = (Button) findViewById(R.id.applyChangeBtn);
applyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updatePictureItem();
ptf.setArray(piArray);
}
});
}
private void updatePictureItem() {
pi.setTitle(text.toString());
piArray.add(pi);
ptf.setArray(piArray);
}
private void SetLocalArray(ArrayList<PictureItem> array) {
this.piArray = array;
}
private PictureItem getPictureItem() {
pi = new PictureItem("", "");
for (int i = 0; i < piArray.size(); i++) {
if (itemID.equals(piArray.get(i))) {
pi = piArray.get(i);
piArray.remove(i);
}
}
return pi;
}}
答案 0 :(得分:0)
我不知道您使用的数组是什么。 通常你不应该依赖片段来获取信息,如果你想将一个对象数组传递给活动,你应该在活动附加中使用Bundle这样做,而不是只传递UUID,也只是传递你需要的阵列。
如果你想要lazy选项,只需创建一个带有静态变量的类来存储片段并在活动中使用它,我不建议。