2016-04-19 19 views
0

これについて多くの投稿がありますが、私は解決策を見つけることができません。私はそれが、私のために見てきたフラグメントの動的置き換えが機能していない

を「OnFragmentInteractionListenerを実装する必要があります」:私は動的活動に断片を交換しようとしている は、私はここにチュートリアルや記事の多くを踏襲していると私は、1つの例外を除いに捕まってしまいました正直なところこれに疲れました。

もっと具体的には、fragmentTransaction.replace(R.id.content_frame,fragment);行で例外が発生します。これはメインアクティビティで見逃したものでなければなりませんが、表示されません。

ここ

私のコードの一部を..です

public class LoggedActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 
private FrameLayout fragment; 

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

    this.setTitle(R.string.app_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.logged, 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); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_Consultas) { 
     android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

     Fragment fragment = new ConsultasFragment(); 
     fragmentTransaction.replace(R.id.content_frame,fragment); 
     fragmentTransaction.commit(); 
     //setContentView(R.layout.activity_main); 
     // Handle the camera action 
    } else if (id == R.id.nav_Trans) { 

    } else if (id == R.id.nav_Pagos) { 

    } else if (id == R.id.nav_Telefono) { 

    } else if (id == R.id.nav_Creditos) { 

    } else if (id == R.id.nav_Mapa) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

}

私のコンテナの活動XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <include 
     layout="@layout/app_bar_logged" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_logged" 
     app:menu="@menu/activity_logged_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

と私のフラグメント活動

public class ConsultasFragment extends Fragment { 
    // 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; 

    public ConsultasFragment() { 
     // Required empty public constructor 
    } 

    /** 
    * Use this factory method to create a new instance of 
    * this fragment using the provided parameters. 
    * 
    * @param param1 Parameter 1. 
    * @param param2 Parameter 2. 
    * @return A new instance of fragment ConsultasFragment. 
    */ 
    // TODO: Rename and change types and number of parameters 
    public static ConsultasFragment newInstance(String param1, String param2) { 
     ConsultasFragment fragment = new ConsultasFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_consultas, container, false); 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

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

    /** 
    * 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 
     void onFragmentInteraction(Uri uri); 
    } 
} 

XML ...

<FrameLayout 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" 
    tools:context="layout.ConsultasFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 

</FrameLayout> 

このコードのほとんどはAndroidスタジオによって自動的に生成されました。

+1

あなたの 'Activity'は' ConsultasFragment.OnFragmentInteractionListener'を実装しなければなりません。現在はそうではありません。 –

答えて

2
次に LoggedActivityでこのメソッドをオーバーライドし

public class LoggedActivity extends AppCompatActivity 
implements NavigationView.OnNavigationItemSelectedListener, ConsultasFragment.OnFragmentInteractionListener 

にごLoggedActivity宣言を変更

@Override 
public void onFragmentInteraction(Uri uri) { 
    // Whatever you would like your activity to do when your fragment calls this 
} 

ここでのポイントは、あなたがあなたのActivityと対話するためにあなたのFragmentためのメカニズムを提供する必要があるということですFragmentmListener.onFragmentInteraction(uri)と電話すると、Activityに電話することができます。どちらの

/** 
* 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 
    void onFragmentInteraction(Uri uri); 
} 

あなたはこのinterfaceを必要としない場合は、fragmentを装着しているにactivityでこのメソッドを実装し、あるいは:

+0

Exeptionはもう表示されませんが、私のフラグメントはロードされていないようですが、このメソッドではどうしたらいいですか? –

+0

私はそれをしました!、ありがとう –

1

あなたConsultasFragmentの下部に、あなたはこのinterface方法が表示されます(これは主に、fragmentactivityとの間でデータのやりとりをすることができるように)、fragmentから削除するだけです。

関連する問題