Android ListFragment App启动错误

时间:2015-09-29 11:23:25

标签: android android-fragments android-listfragment

我只是创建一个简单的List片段,但我的应用程序不能在AVD上运行。可以找出错误。 这是我的MainActivity.java

package in.blogspot.food_n_moreblog.recipes; 公共类MainActivity扩展Activity实现catagorylistfrag.OnFragmentInteractionListener {

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

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

    return super.onOptionsItemSelected(item);
}
@Override
public void itemClicked(long id){
    catagorydetailfragment detailfragment=new catagorydetailfragment();
    FragmentTransaction ft=getFragmentManager().beginTransaction();
    detailfragment.setWorkout(id);
    ft.replace(R.id.fragment_container,detailfragment);
    ft.addToBackStack(null);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.commit();
}

}

DetailFragment(java code) package in.blogspot.food_n_moreblog.recipes;

public class catagorydetailfragment扩展Fragment {

private long workoutID;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
    if (savedInstanceState!=null)
    {
        workoutID=savedInstanceState.getLong("workoutID");
    }
    return inflater.inflate(R.layout.catagorydetails,container,false);
}

@Override
public void onStart()
{
    super.onStart();
    View view=getView();
    if(view!=null){
        TextView title=(TextView)view.findViewById(R.id.titleText);
        CatagoryValues value=CatagoryValues.catagories[(int)workoutID];
        title.setText(value.getName());
        TextView des=(TextView)view.findViewById(R.id.detailText);
        des.setText(value.getDescription());
    }
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    savedInstanceState.putLong("workoutID",workoutID);
}
public void setWorkout(long id){
    this.workoutID=id;
}

}

列出项目片段java代码

package in.blogspot.food_n_moreblog.recipes; public class catagorylistfrag扩展了ListFragment {

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;


// TODO: Rename and change types of parameters
public static catagorylistfrag newInstance(String param1, String param2) {
    catagorylistfrag fragment = new catagorylistfrag();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public catagorylistfrag() {
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
   // super.onCreate(savedInstanceState);

    /*if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }*/
    String[] names=new String[CatagoryValues.catagories.length];
    for(int i=0;i<names.length;i++){
        names[i]=CatagoryValues.catagories[i].getName();
    }

    // TODO: Change Adapter to display your content
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(inflater.getContext(),android.R.layout.simple_expandable_list_item_2,names);
    setListAdapter(adapter);
    return super.onCreateView(inflater,container,savedInstanceState);
}


@Override
public void onAttach(Context context) {
  /*  super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }

* / super.onAttach(上下文);

    Activity a;

    if (context instanceof Activity){
        a=(Activity) context;
        this.mListener=(OnFragmentInteractionListener)a;
    }

}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    if (null != mListener) {
        // Notify the active callbacks interface (the activity, if the
        // fragment is attached to one) that an item has been selected.
        mListener.itemClicked(id);
    }
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p/>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
   // public void onFragmentInteraction(String id);
    void itemClicked(long id);
}

}

1 个答案:

答案 0 :(得分:0)

在列表项Fragment java code:

中尝试此数组适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>(inflater.getContext(),android.R.layout.android.R.layout.simple_list_item_1,names);