2014-11-13 6 views
1

내 코드에서 두 가지 오류가 발생합니다.이진 XML 파일 줄 # 11 : 클래스 조각을 부 풀릴 때 오류가 발생했습니다.

발생 원인 : java.lang.IllegalStateException : Fragment com.example.dfoley.write_to_file.topFragment가 뷰를 작성하지 않았습니다.

원인 : android.view.InflateException : 이진 XML 파일 줄 # 11 : 줄 MainActivity.java:21을 가리키는 클래스 조각을 모두 팽창하는 동안 오류가 발생했습니다. setContentView (R.layout.activity_main);

bottomFragment

package com.example.dfoley.write_to_file; 
import android.app.ListFragment; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 

import java.util.ArrayList; 

public class bottomFragment extends ListFragment { 
    private ArrayAdapter<StateUser> adapter; 

    @Override 
    public void onActivityCreated(Bundle saveInstanceState){ 
     ArrayList<StateUser> flight = MainContoller.getInstance().getFlights(); 
     this.adapter = new ArrayAdapter<StateUser>(getActivity(), android.R.layout.simple_list_item_1, flight); 
     setListAdapter(this.adapter); 
     super.onActivityCreated(saveInstanceState); 
    } 
    public void refreshList(){ 
     this.adapter.notifyDataSetChanged(); 
    } 
} 

최고 조각

package com.example.dfoley.write_to_file; 

import android.app.Activity; 
import android.app.Fragment; 
import android.content.Context; 
import android.os.Bundle; 
import.android.util.Log; 
import android.view.View; 
import.android.widget.Button; 
import android.widget.EditText; 

import java.io.IOException; 
import java.io.OutputStreamWriter; 



public class topFragment extends Fragment{ 
    private FlightSearcher searcher; 
    EditText text1; 

    public interface FlightSearcher { 
     public void refreshFlightList(); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     searcher = (FlightSearcher) activity; 
     super.onAttach(activity); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState){ 
     setupListeners(); 
     super.onActivityCreated(savedInstanceState); 
    } 

    public void setupListeners() { 
     Button addUser = (Button)getActivity().findViewById(R.id.button); 
     addUser.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       writeToFile(); 
       searcher.refreshFlightList(); 
      } 
     }); 
    } 

    private void writeToFile() { 
     text1=(EditText)getActivity().findViewById(R.id.editText); 
     String AddUsers = text1.getText().toString(); 
     try { 
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(getActivity().openFileOutput("UserList", Context.MODE_PRIVATE)); 
      outputStreamWriter.write(AddUsers); 
      outputStreamWriter.close(); 
     } 
     catch (IOException e) { 
      Log.e("Exception", "File write failed: " + e.toString()); 
     } 
    } 
} 

주요 활동

package com.example.dfoley.write_to_file; 

import android.app.FragmentManager; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends FragmentActivity implements topFragment.FlightSearcher{ 

    public void refreshFlightList() { 
     FragmentManager mgr = getFragmentManager(); 
     bottomFragment bottomFragmentRef =(bottomFragment) mgr.findFragmentById(R.id.bottom_fragment); 
     bottomFragmentRef.refreshList(); 
    } 

    @Override 
    public 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); 
    } 
} 

activiy_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity"> 

<fragment 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:name="com.example.dfoley.write_to_file.topFragment" 
    android:id="@+id/top_fragment" 
    android:layout_weight="1" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    tools:layout="@layout/topfragment" /> 

<fragment 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:name="com.example.dfoley.write_to_file.bottomFragment" 
    android:id="@+id/bottom_fragment" 
    android:layout_weight="1" 
    android:layout_below="@+id/top_fragment" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    tools:layout="@layout/bottomfragment" /> 

답변

2

두 조각 모두보기를 만드는 방법을 알려주지 않습니다. 나는 당신이 도구를 사용하고있는 것을 본다 : layout 태그, 그러나 도구 문서에 따르면, 그것은 디자이너에게 힌트 일 뿐이다.

"이 속성은 일반적으로 태그에 설정되며 레이아웃에서 렌더링 할 레이아웃을 기록하는 데 사용됩니다 (런타임에는 조각 클래스의 작업에 따라 결정됩니다) 태그로 표시). "

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

따라서 당신은을 반환 한 후, onCreateView를 오버라이드 (override) 뷰 계층을 팽창하고, 필요

관련 문제