2017-10-10 6 views
0

Je suis nouveau avec le développement Android, donc c'est probablement une erreur de débutant, mais je suis bloqué.Impossible de démarrer l'activité, aucune vue trouvée pour l'ID

Je veux commencer une activité en cliquant sur le menu, mais l'exception suivante apparaît:

java.lang.RuntimeException: Impossible de démarrer l'activité ComposantInfo {com.mygymroutine.mygymroutine/com. mygymroutine.mygymroutine.activities.ActivitySchedule}: java.lang.IllegalArgumentException: Pas de vue trouvée pour id 0x7f0d008b (com.mygymroutine.mygymroutine: id/fragment_schedule) pour le fragment ScheduleFragment {39f1839 # 0 id = 0x7f0d008b ...

C'est le code lié:

public class basic extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_basic); 
     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); 
     displaySelectedScreen(R.id.nav_schedule); 
    } 

    @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.basic, 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); 
    } 
public boolean onNavigationItemSelected(MenuItem item) { 
     displaySelectedScreen(item.getItemId()); 
     return true; 
    } 
    private void displaySelectedScreen(int itemId) { 
     Intent intent=null; 
     switch (itemId) { 
      case R.id.nav_schedule: 
       intent=new Intent(this, ActivitySchedule.class); 
       startActivity(intent); 
       break; 
     } 

Code d'activité

package com.mygymroutine.mygymroutine.activities; 


import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 


import com.mygymroutine.mygymroutine.Fragments.ScheduleFragment; 
import com.mygymroutine.mygymroutine.R; 


public class ActivitySchedule extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.acivity_schedule); 
     if(savedInstanceState==null){ 
      ScheduleFragment fg = new ScheduleFragment(); 
      getSupportFragmentManager().beginTransaction().add(R.id.fragment_schedule, fg) 
        .commit(); 
     } 
    } 
} 

Fragment

package com.mygymroutine.mygymroutine.Fragments; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import com.mygymroutine.mygymroutine.R; 

public class ScheduleFragment extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     //returning our layout file 
     //change R.layout.yourlayoutfilename for each of your fragments 
     View rootView=inflater.inflate(R.layout.fragment_schedule, container, false); 
     FloatingActionButton fab=(FloatingActionButton)rootView.findViewById(R.id.fab_schedule); 
     return inflater.inflate(R.layout.fragment_schedule, container, false); 
    } 


    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     getActivity().setTitle("Schedule"); 
    } 
} 

Fragment annexe xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/fragment_schedule" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="17dp" 
     android:text="Schedule" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_schedule" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginBottom="41dp" 
     android:layout_marginEnd="32dp" 
     android:layout_marginRight="32dp" 
     android:clickable="true" 
     android:tint="@android:color/background_light" 
     app:backgroundTint="@android:color/holo_red_light" 
     app:fabSize="mini" 
     app:rippleColor="@android:color/transparent" 
     app:srcCompat="@android:drawable/ic_menu_add" /> 

</RelativeLayout> 

calendrier Activité xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.mygymroutine.mygymroutine.activities.ActivitySchedule"> 

</android.support.constraint.ConstraintLayout> 
+2

Votre Fragment devrait revenir 'rootView', par la façon dont le changement –

+0

'return inflater.inflate (R.layout.fragment_schedule, container, false);' retourner 'rootView;' – UltimateDevil

+0

Dans le XM L, où est situé 'R.id.fragment_schedule'? Quel fichier? –

Répondre

0

ici:

getSupportFragmentManager().beginTransaction().add(R.id.fragment_schedule, fg) 
       .commit(); 

Vous passez R.id.fragment_schedule comme conteneur du fragment. Où le fragment sera inséré. Cependant, R.id.fragment_schedule fait partie de votre Fragment. Donc, il n'était pas encore gonflé. Vous ne pouvez pas l'utiliser comme conteneur car il n'a pas encore été créé.

Je pense que vous voulez est:

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.mygymroutine.mygymroutine.activities.ActivitySchedule" 

    <!-- ADD LINE BELOW --> 
    android:id="@+id/fragment_container" > 

</android.support.constraint.ConstraintLayout> 

Et changer ceci:

getSupportFragmentManager().beginTransaction().add(R.id.fragment_schedule, fg) 
       .commit(); 

à ceci:

getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fg) 
       .commit(); 
+0

Maintenant, cela fonctionne, merci beaucoup! –

0

In. La méthode onOptionsItemSelected ajouter ce qui suit (Cette méthode apporte un paramètre - point MenuItem)

switch (item.getItemId()) 
    case R.id.myFirst 
     startActivity(new Intent(this, MyFirstActivity.class)); 
    case R.id.mySecond 
     startActivity (newIntent(this, MySecondActivity.class)); 

Et ainsi de suite. Vous pouvez définir une mise en page pour l'activité avec les vues nécessaires. Vous pouvez également déclarer l'activité dans le fichier manifeste.

Cela fonctionne. Je commence mes activités de la principale comme ça.