这两种类型的初始化在片段中有什么区别

时间:2013-08-22 15:07:27

标签: android android-fragments

我正在尝试从Activity的onCreate()方法初始化listfragment中列表的内容。

活动代码的一部分:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dish);
            FragmentTransaction transaction1 = getSupportFragmentManager()
                        .beginTransaction();
                    mDishListFragment = new DishListFragment();
                transaction1.add(R.id.dishList_fragment, mDishListFragment, "DishListFragment");
                transaction1.commit();

mDishListFragment.showItem(totalList.get(0));
    }

片段代码的一部分:

BinderData adapter = null;
    List<HashMap<String, String>> dishDataCollection;

@Override
public void onCreate(Bundle savedInstanceState) {
        Log.e("emenu", "Dishlistfragment on create");
        super.onCreate(savedInstanceState);
        adapter = new BinderData(getActivity(), R.id.article);
    }
public void showItem(List<HashMap<String, String>> map) {
        dishDataCollection = map;       
        if (dishDataCollection != null) {
            adapter.init(dishDataCollection); //NullPOinterException
        }   
        setListAdapter(adapter);
    }   

初始化自定义适配器时出现nullpointerException。

现在,当我在活动中以不同的方式初始化我的片段时,此代码完全正常。像这样:

mDishListFragment = (DishListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.dishList_fragment);

而不是初始化我在上面的第一个代码中所做的方式。

我需要使用片段事务add()方法,因为我需要能够隐藏并在需要时显示片段。

这两种类型的初始化有什么区别?为什么前者不能正常工作?

希望你们能提供帮助。如果您需要代码的任何其他部分,请告诉我。

供您参考,适配器代码:

LayoutInflater inflater;
    ImageView thumb_image;
    List<HashMap<String, String>> dishDataCollection;
    ViewHolder holder;

    public BinderData(Context context, int list_row) {
        // super(context , list_row , map);
        super(context, list_row);
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void init(List<HashMap<String, String>> map) {
        this.dishDataCollection = map;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        // return idlist.size();
        return dishDataCollection.size();
    }


    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView != null)
            if (position == mItemIndex) {
                convertView.setSelected(true);
                convertView.setPressed(true);
                convertView.setBackgroundResource(R.drawable.gradient_bg_hover);
            } else {
                convertView.setBackgroundResource(R.drawable.gradient_bg);
            }

        View vi = convertView;

        if (convertView == null) {
            vi = inflater.inflate(R.layout.list_row, parent, false);
            holder = new ViewHolder();

            holder.name = (TextView) vi.findViewById(R.id.dish); // city name
            holder.rate = (TextView) vi.findViewById(R.id.rate); // city weather
                                                                    // overview
            holder.shortdescription = (TextView) vi
                    .findViewById(R.id.shortdescription); // city temperature
            holder.dishImage = (ImageView) vi.findViewById(R.id.list_image); // thumb
                                                                                // image
                                                                                // vi.setActivated(true);
            vi.setTag(holder);
        } else {

            holder = (ViewHolder) vi.getTag();
        }

        // Setting all values in listview

        holder.name.setText(dishDataCollection.get(position).get(KEY_NAME));
        holder.rate.setText(dishDataCollection.get(position).get(KEY_RATE));
        holder.shortdescription.setText(dishDataCollection.get(position).get(
                KEY_SHORT));

        // Setting an image
        String uri = "drawable/"
                + dishDataCollection.get(position).get(KEY_ICON);
        int imageResource = vi
                .getContext()
                .getApplicationContext()
                .getResources()
                .getIdentifier(
                        uri,
                        null,
                        vi.getContext().getApplicationContext()
                                .getPackageName());
        Drawable image = vi.getContext().getResources()
                .getDrawable(imageResource);
        holder.dishImage.setImageDrawable(image);

        return vi;
    }

    /*
     * 
     * */
    static class ViewHolder {

        TextView name;
        TextView rate;
        TextView shortdescription;
        ImageView dishImage;
    }

    public void setSelectItem(int index) {
        mItemIndex = index;
    }

    public int getSelectItem() {
        return mItemIndex;
    }

2 个答案:

答案 0 :(得分:0)

片段onCreate()中的

您的视图尚不存在。你应该把你的初始化代码移到onCreateView()。熟悉片段生命周期:http://developer.android.com/guide/components/fragments.html#Lifecycle

答案 1 :(得分:0)

您可能需要对View进行膨胀而不是setContentView(R.layout.activity_dish);

   View view = inflater.inflate(R.layout.fragment_rsslist_overview,
    container, false);

示例

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_rssitem_detail,
        container, false);
    return view;
  }

  public void setText(String item) {
    TextView view = (TextView) getView().findViewById(R.id.detailsText);
    view.setText(item);
  }
}