onAttach()活动参数null?

时间:2014-06-22 21:44:27

标签: android android-activity android-fragments

我有一个主活动类,以及主活动中的私有内部类。私有内部类具有调用时将显示片段的方法。这个内部类实现了Fragment类中定义的接口,用作一种回调。通过代码展示可能最简单。

public class MainActivity extends FragmentActivity {

//on a button clicked
EditItemManger em = new EditItemManager();
em.begin();



private class EditItemManager implements on EditItemFragment.EditedItemClickedListener{

//consructor, other stuff. no onCreate method because this inner class does not (explicity??) extend activty

public void begin(){
EditItemFragment editItemFrag = new EditItemFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(editItemFrag, EDIT_FRAG_TAG)
            fragmentTransaction.commit();
}

@Override
        public void onEditItemClicked() {
            editFinish();
        }


public void editFinish()
{
 // other stuff
}

}

}

我的EditItemFragment类,其中onAttach方法始终具有空活动参数

public class EditItemFragment extends DialogFragment {
    protected EditedItemClickedListener editedItemClickedListener;
    protected ImageButton button;

    public EditItemFragment(){}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             final Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.edit_name_fragment, container, false);
        button = (ImageButton) view.findViewById(R.id.submit_new_item_button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
                public void onClick(View view) {
                        editedItemClickedListener.onEditedItemButtonClicked();
                }
            });

            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
                try {
                    editedItemClickedListener= (EditedItemClickedListener) activity;
                } catch (ClassCastException e) {
                    e.printStackTrace();
                }
            }

            public interface OnEditNameButtonClickedListener {
                public void onEditNameButtonClicked();
            }

因为我的Fragment类中onAttach()中的参数始终为null,所以最终会导致空指针异常。我想知道是否因为片段是从一个没有扩展活动的类调用的。问题是,如果此类扩展活动,则尝试提交片段事务

时会出现问题

1 个答案:

答案 0 :(得分:0)

我访问你的onAttach返回nullpointer异常。因为你的父Activity活动主要活动没有实现你的自定义接口所以它返回null。请参阅以下代码,如果有帮助,请告诉我:

    public class MainActivity extends FragmentActivity implements EditItemFragment.EditedItemClickedListener{ 
       private DialogFragment editItemDialog;
     //Do your code here
       @Override
        public void onEditItemClicked() {
            editFinish();
        }

      public void showDialog(){
           //this is for showing your custom dialog fragment
         editItemDialog = EditItemFragment.newInstance();
         editItemDialog.show(getSupportFragmentManager(),"editItemDialog");
      }

}

这适用于您的EditItemFragment:

    public class EditItemFragment extends DialogFragment{
       //Do your code here

    public static EditItemFragment newInstance(){
         EditItemFragment editItemDialog = new EditItemFragment();
         return editItemDialog;
     }

}