【发布时间】:2015-01-26 13:15:24
【问题描述】:
我遇到了应用程序因空点异常而崩溃的问题。 我知道它在尝试从 pictureTalkFragment 获取 ArrayList 时崩溃。在此类中仅设置为 PictureTalkFragment ptf; 换句话说,我试图从一个类而不是该类的实例中获取一个元素(在 ptf 中为 arraylist 提供 getter/setter,并将 arraylist 作为替代方案公开)。
但我只是想弄清楚如何正确处理获取类之间的实例(活动--->片段和返回等)。在Java中,我通常只是在构造函数中有一个引用,它在创建新类时发送了实例/引用。但是在Android中,所有这些onCreate(getActivity,getContext ++),我很困惑:P何时用户在哪里以及如何使用:(
EditPicture 是从从 PictureTalkFragment 扩展的 GridViewAdapter 中的这段代码开始的(在 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;
编辑图片类
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;
}}
【问题讨论】:
-
我不太明白问题出在哪里,最好的办法是发布堆栈跟踪及其识别问题所在的行。
-
我没有收到任何崩溃错误,除了 Picture talk 已停止工作。调试器/logcat 等没有输出。当我通过代码给自己设置断点时,它在 SetLocalArray(ptf.getArray()); 上崩溃;在 EditPicture 类中。我的猜测是我试图从一个类而不是这个类的实例中获取一些东西。我想知道我应该如何获得 PictureTalkFragment 的实例。
-
你还没有实例化片段,把'ptf = new PictureTalkFragment();'在 SetLocalArra 之前的行中,我认为您使用的片段不正确,但您现在应该得到一个不同的错误。
-
这就是我的意思,片段是更早实例化的,毕竟 EditPicture 是从片段实例启动的。设置 ptf = new PictureTalkFragment 只会返回一个空片段,而不是片段的初始实例。我在如何让 ptf 成为 PictureTalkFragment 实例的引用而不是新的/未实例化的 PictureTalkFragment 方面遇到问题
-
但它没有被实例化,据我所知......你创建变量'private PictureTalkFragment ptf;'然后在创建时,尝试访问它,而不是在任何地方实例化。
标签: android android-activity arraylist android-context