旋转后片段字段为空

时间:2016-04-28 10:47:47

标签: android android-fragments nullpointerexception rotation fragment

我有片段,我调用自定义视图类(Dragger)

public class CanvasFragment extends Fragment {

    private Dragger dragger;

    public CanvasFragment() {
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_canvas, container, false);

        dragger = (Dragger) rootView.findViewById(R.id.dragger);

        int[] symbolIndex = {};

        dragger.setElementsArray(symbolIndex);
        return rootView;
    }

    public void addIconToCanvas(int id){
        dragger.addIcon(id);
    }
}

我还有另一个包含RecyclerView和适配器的片段,我通过CanvasFragment's的主机活动调用addIconToCanvas()方法CanvasFragment

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {

    private int[] data;
    private Context context;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView itemView;
        public ViewHolder(View v) {
            super(v);
            itemView = (ImageView) v.findViewById(R.id.im_item);
        }
    }

    public ItemAdapter(Context context, int[] myDataset) {
        data = myDataset;
        this.context = context;
    }

    @Override
    public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.itemView.setImageResource(data[position]);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            ((MainActivity)context).addItemToCanvas(data[position]);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.length;
    }
}

我发现dragger中的CanvasFragment轮换后获得了新的引用,但是当我从适配器调用addIconToCanvas()时,dragger为空。

我真的无法理解为什么字段会引用然后丢失它)))

有人知道吗,这有什么问题?

提前致谢!

P.S。:主持人活动

public class MainActivity extends AppCompatActivity {

    private CanvasFragment canvasFragment = new CanvasFragment();
    private ItemsFragment itemsFragment = new ItemsFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container_canvas, canvasFragment)
                .commit();
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container_items, itemsFragment)
                .commit();
        }

    }

    public void addItemToCanvas(int id){
        canvasFragment.addIconToCanvas(id);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

3 个答案:

答案 0 :(得分:1)

删除行setRetainInstance(true);从你的碎片中试试。

答案 1 :(得分:1)

使用

  

private CanvasFragment canvasFragment = new CanvasFragment();

每次创建Fragment时,都会将Activity的新实例分配给变量 canvasFragment 。但只有savedInstanceState == null将CanvasFragment的这个实例添加到UI。

只有在需要显示Fragment时才会调用onCreateView()方法Fragment,因此配置后 dragger 对于新创建的实例保持为null变化

你能做什么?

如果要将CanvasFragment添加到UI,则只创建它的实例。在savedInstanceState != null的情况下,找到已存在的Fragment,如下所示:

canvasFragment = (CanvasFragment)getSupportFragmentManager().findFragmentById(R.id.container_canvas);

答案 2 :(得分:0)

把这个:

androidConfigChanges: "keyboardHidden | orientation | screenSize"

在android xml文件中定义了mainActivity。像这样的东西

<activity
android:name="com.projectname.MainActivity"
android:label = "@string/title_activity_main"
</activity>
那么它应该运气如下:

<activity
android:name="com.projectname.MainActivity"
androidConfigChanges: "keyboardHidden | orientation | screenSize"
android:label = "@string/title_activity_main"
</activity>