2015-09-29 189 views
0

我只是创建一个简单的List-fragment,但我的应用程序不会运行在AVD上。无法找出错误。 这是我的MainActivity.javaAndroid ListFragment应用程序启动错误

包in.blogspot.food_n_moreblog.recipes; 公共类MainActivity延伸活动实现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代码) 包in.blogspot.food_n_moreblog.recipes;

公共类catagorydetailfragment延伸片段{

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代码

包in.blogspot.food_n_moreblog.recipes; 公共类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); 
} 

}

+0

什么logcat的说?你会得到什么错误信息? – Prexx

+0

“/ AndroidRuntime:致命例外:主要 进程:in.blogspot.food_n_moreblog.recipes,PID:6965 java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView” –

+0

这就是我的日志说的... :( –

回答

0

尝试在你的列表项的段Java代码,这个阵列适配器:

ArrayAdapter<String> adapter=new ArrayAdapter<String>(inflater.getContext(),android.R.layout.android.R.layout.simple_list_item_1,names); 
相关问题