마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

Fragment에서 LIstView 이용 시 질문드립니다.

0 추천

원래 개발 하려는 화면은 여섯탭에 각 프래그먼트에 리스트 뷰 / 하단에 버튼 하나

이렇게 하고 싶은데 프래그먼트에 리스트뷰를 써본적이 없어서 에러가 나는데 질문 드립니다.

우선 리스트뷰는 가장 기본형으로 먼저 해보고 있습니다. 추후에 커스텀 리스트뷰를 쓸 계획입니다.

public class MainActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		FragmentManager fm = getSupportFragmentManager();
		FragmentList frag = new FragmentList();
		FragmentTransaction transaction = fm.beginTransaction();
		
		transaction.replace(R.id.fragment_container, frag);
		transaction.addToBackStack(null);
		
		transaction.commit();
		
	}

	
}

public class FragmentList extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View view = inflater.inflate(R.layout.fragment_list, container,false);
		listView = (ListView)getActivity().findViewById(R.id.list);
		
		String[] items = new String[]{"item1" , "item2", "item3"};
		mAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1,items);
		
		listView.setAdapter(mAdapter);
		
		
		return view;
	}
	private ListView listView;
	private ArrayList<String> item;
	private ArrayAdapter<String> mAdapter;
}

메인 xml 입니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.fragmentlistview.MainActivity" >

	<fragment 
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:id="@+id/fragment_container"/>

</LinearLayout>

프래그먼트에 사용 할 레이아웃입니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
   <ListView 
       android:id="@+id/list"
       android:layout_height="match_parent"
       android:layout_width="match_parent"></ListView>

   <Button 
       android:id="@+id/more_btn"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="more"/>
   
</LinearLayout>

Log 제일 첫 에러인데 무슨 의미인지 잘 모르겠습니다.

01-30 23:20:52.301: E/AndroidRuntime(1860): FATAL EXCEPTION: main
01-30 23:20:52.301: E/AndroidRuntime(1860): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentlistview/com.example.fragmentlistview.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
 

xml Fragment에 class를 지정해야 하나요 ??? 제가 나중엔 6개의 프래그먼트를 쓸건데

xml fragment 영역에 클래스를 지정하면 여러개를 어떻게 쓸수 있는지 궁금합니다... 가르침 부탁드립니다.

seungdols (170 포인트) 님이 2015년 1월 30일 질문

1개의 답변

0 추천

listView = (ListView)getActivity().findViewById(R.id.list);

=>

listView = (ListView) view.findViewById(R.id.list);

nicehee (73,100 포인트) 님이 2015년 1월 31일 답변
그래도 에러가 나는데 어떻게 해야하죠 ???
그럼 에러로그를 올려보셔요
같은 코드를 다른 프로젝트에서 실행하니까 되네요 ;;
감사합니다.
...