2016-05-13 94 views
2

您好目前我正在Android Studio上使用FragmentPagerAdapter切換片段,每個片段都使用根片段,因此當我使用一個片段時,它可以很好地工作,但是當我想實現第二個時,它給了我下面的錯誤;Android - 沒有找到片段,更改片段的視圖

java.lang.IllegalArgumentException: No view found for id 0x7f0c00ae (net.beardboy.aarcapp:id/root_frame_graphs) for fragment GraphsFragment{d178606 #0 id=0x7f0c00ae} 

我片段之間交換使用getSupportFragmentManager(),正如我所說的它工作得很好,我第一次使用,但隨後它給了我錯誤。這是我的代碼:

public class MainActivity extends AppCompatActivity implements ReportsAdapter.ReportCallBacks { 

    ProgressDialog progressDialogReports; 
    ProgressDialog progressDialogGraphs; 
    ProgressDialog progressDialogSettings; 

    DrawerLayout drawerLayout; 
    ViewPager viewPager; 
    FragmentPagerAdapter fragmentPagerAdapter; 
    Toolbar toolbar; 
    User currentUser; 
    SessionStateManager sessionStateManager; 
    RequestQueue requestQueue; 
    ReportsFragments reportsFragments; 
    GraphsFragment graphsFragment; 

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

     sessionStateManager = new SessionStateManager(this); 
     currentUser = new User(); 
     currentUser = sessionStateManager.getCurrentUser(); 
     requestQueue = Volley.newRequestQueue(this); 
     graphsFragment = new GraphsFragment(); 


     toolbar = (Toolbar) findViewById(R.id.toolbar_main); 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabBarLayout); 
     viewPager = (ViewPager) findViewById(R.id.viewPager); 
     setSupportActionBar(toolbar); 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
     fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) { 
      private static final int FRAGMENT_COUNT = 3; 

      @Override 
      public Fragment getItem(int position) { 
       switch (position) { 
        case 0: 

         try { 
          getReportsdata(); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
         return new RootFragmentReports(); 
        case 1: 
          getGraphsdata(); 

         return new RootFragmentGraphs(); 
        case 2: 
         return new RootFragmentSettings(); 
       } 


       return null; 
      } 

      @Override 
      public int getCount() { 
       return FRAGMENT_COUNT; 
      } 

      @Override 
      public CharSequence getPageTitle(int position) { 

       switch (position) { 
        case 0: 
         return "Reportes"; 
        case 1: 
         return "Graficas"; 
        case 2: 
         return "Pagos"; 
        default: 
         return null; 
       } 
      } 
     }; 

     viewPager.setOffscreenPageLimit(3); 
     viewPager.setAdapter(fragmentPagerAdapter); 
     tabLayout.setupWithViewPager(viewPager); 

     viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
       invalidateOptionsMenu(); 
      } 

      @Override 
      public void onPageSelected(int position) { 
       invalidateOptionsMenu(); 
      } 

      @Override 
      public void onPageScrollStateChanged(int state) { 

      } 

     }); 


    } 


    public void getReportsdata() throws JSONException { 

     progressDialogReports = ProgressDialog.show(MainActivity.this, getString(R.string.please_wait), getString(R.string.downloading_data), true, false); 


     JSONObject jsonUser = new JSONObject(); 
     JSONObject jsonMain = new JSONObject(); 
     jsonUser.put(JSONKeys.KEY_ACCESS_TOKEN, currentUser.getAccess_token()); 
     jsonUser.put(JSONKeys.KEY_CLIENTE_ID, currentUser.getCilenteId()); 
     jsonUser.put(JSONKeys.KEY_NAME, currentUser.getName()); 
     jsonUser.put(JSONKeys.KEY_EMAIL, currentUser.getEmail()); 
     jsonMain.put(JSONKeys.KEY_AARC_USER, jsonUser); 

     JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, JSONKeys.URL_REPORTES_POR_CLIENTE, jsonMain, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       progressDialogReports.dismiss(); 

       try { 
        ArrayList<JSONReport> jsonReports = (ArrayList<JSONReport>) JSONParser.parseJSONReport(response); 
        replaceFragment(jsonReports); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
     }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         progressDialogReports.dismiss(); 
         Log.i("errr", error.toString()); 
         DialogUtil.createSimpleDialog(MainActivity.this, getString(R.string.error_downloading_data), error.toString()).show(); 

        } 
       }); 
     int socketTimeout = 180000;//3 minutes - change to what you want 
     RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); 
     jsonObjectRequest.setRetryPolicy(policy); 
     requestQueue.add(jsonObjectRequest); 


    } 

    public void getGraphsdata() { 

     ArrayList<Graph> graphArrayList = new ArrayList<>(); 

     for (int i = 0; i < 10; i++) { 
      Graph graficas = new Graph(); 
      graficas.setAnalisis(i); 
      graficas.setSucursal("descripcion" + i); 
      graphArrayList.add(graficas); 
     } 




     ArrayList<Entry> entries = new ArrayList<>(); 

     entries.add(new Entry(2f, 0)); 
     entries.add(new Entry(4f, 1)); 
     entries.add(new Entry(3f, 2)); 
     entries.add(new Entry(4f, 3)); 


     LineDataSet dataset = new LineDataSet(entries, "Fecha"); 

     ArrayList<String> labels = new ArrayList<String>(); 
     labels.add("January"); 
     labels.add("February"); 
     labels.add("January"); 
     labels.add("February"); 

     replaceFragmentGraph(graphArrayList); 

    } 

    private void replaceFragment(ArrayList<JSONReport> jsonReports) { 
     reportsFragments = new ReportsFragments(); 
     ReportsAdapter reportsAdapter; 
     reportsAdapter = new ReportsAdapter(MainActivity.this, jsonReports); 
     reportsAdapter.setReportAdapterCallBacks(MainActivity.this); 
     reportsFragments.setComplaintsAdapter(reportsAdapter); 
     getSupportFragmentManager() 
       .beginTransaction() 
       .replace(R.id.root_frame_actividades, reportsFragments) 
       .commit(); 

    } 


    private void replaceFragmentGraph(ArrayList<Graph> datos) { 

     graphsFragment = new GraphsFragment(); 
     //Bundle bundle=new Bundle(); 
     //bundle.putParcelable("datos", (Parcelable) datos); 
     //graphsFragment.setArguments(bundle); 

     getSupportFragmentManager() 
       .beginTransaction() 
       .replace(R.id.root_frame_graphsa, graphsFragment) 
       //.addToBackStack(null) 
       .commit(); 


    } 


    @Override 
    public void OnReportCallBacks(JSONReport jsonReport) { 

     Intent intent = new Intent(this, ReportsDetailActivity.class); 
     intent.putExtra(JSONKeys.KEY_FOLIOB, jsonReport.getFoliob()); 
     startActivity(intent); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_acitivity_main, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     Intent intent = new Intent(this, SettingsActivity.class); 
     startActivity(intent); 
     return super.onOptionsItemSelected(item); 
    } 


} 

錯誤開始時,我在getSupportFragmentManager()的添加GraphsFragment出現,我仔細檢查了佈局的ID是正確的參數等,但由於在錯誤的ID 0x7f0c00ae後來在同一行上再次出現同一個id,原諒我的英文不好。謝謝你們。

編輯 我GraphsFragment類

public class GraphsFragment extends Fragment { 
LineChart chart; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootview = inflater.inflate(R.layout.fragment_graphs, container, false); 

    chart = (LineChart) rootview.findViewById(R.id.linechart); 

    return rootview; 
} 

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


    //code 


} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
} 


} 

fragments_graphs.xml

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

    <com.github.mikephil.charting.charts.LineChart 
     android:id="@+id/linechart" 
     android:layout_width="match_parent" 
     android:layout_height="220dp" 
     android:layout_margin="5dp" /> 

</RelativeLayout 

RootFragmentGraphs類

public class RootFragmentGraphs extends Fragment{ 
    private static final String TAG = "RootFragmentGraphs"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_root_fragment_graficas, container, false); 



     return view; 
    } 


} 

fragment_root_fragment_graficas.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/root_frame_graphs" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".fragments.RootFragmentGraphs"> 

</FrameLayout> 
+0

請你能添加代碼爲GraphsFragment – Omar

+0

代碼添加@Omar – miguelacio

+1

能否請您包括代碼爲您活動的onCreate,然後還包括XML你在哪裏宣佈你的碎片? – ishmaelMakitla

回答

0

問題可能是因爲您錯誤地佈局了(XML) - 意味着R.id.root_frame_graphs可能不在該佈局文件中。 人們期望(使用FrameLayout)找到像這樣在你的佈局文件:

<FrameLayout android:id="@+id/root_frame_graphs"    
    android:... 
    android:layout_height="match_parent" /> 

傳遞到FragmentTransaction.replace(該ID(R.id.root_frame_graphs))必須的setContentView()指定的佈局的孩子 - 這我建議你在你的問題中加入。我的猜測是,你可能會面臨着同樣的問題,因爲一個解決here

+0

雖然您可能正確地使用了錯誤的佈局文件被誇大的概念,但在使用上下文時,'具體而言,您必須在XML文件中同時具有這兩個碎片'替換()'他們取消了整個答案的資格 –

+0

@MarcinOrlowski,我理解你的觀點 - 但也許我被他指的是兩個不同的ID--'root_frame_actividades'和'root_frame_graphs'這個事實所誤導 - 這讓我覺得他有兩個「擁有者」......我希望這是有道理的。 – ishmaelMakitla

+0

但您的答案仍然以我引用的句子爲特徵,因爲您無法刪除未從代碼中添加的片段,所以這只是錯誤的。 –