2017-01-03 67 views
0

我見過有同樣問題的人,他們說,.replace()有問題,我已經用fragmentManager.beginTransaction()構造了片段和容器的寫法。replace (R.id.container,new Fragment())。commit(); 而且我必須以其他方式做到這一點,我將其實現到我的代碼中,但我仍然得到了錯誤。 錯誤:我該如何解決:沒有找到id爲0x7f0d00aa的視圖?

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: mcshreker.pocketmath, PID: 3215 
       java.lang.IllegalArgumentException: No view found for id 0x7f0d00aa (mcshreker.pocketmath:id/content_main_container) for fragment secondFragment{bf0f022 #0 id=0x7f0d00aa} 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1292) 
        at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323) 
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136) 
        at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092) 
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998) 
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

代碼(主要活動)

package mcshreker.pocketmath; 

import android.app.FragmentTransaction; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.annotation.RequiresApi; 
import android.support.v4.app.FragmentManager; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

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


@RequiresApi(api = Build.VERSION_CODES.M) 
@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 
return super.onOptionsItemSelected(item); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 

    if (id == R.id.nav_first_layout) { 
     fragmentManager.beginTransaction().replace 
       (R.id.content_main_container, new firstFragment()).commit(); 

     // Handle the camera action 

} 

    } else if (id == R.id.nav_second_layout) { 
     fragmentManager.beginTransaction().replace 
       (R.id.content_main_container, new secondFragment()).commit(); 
    } else if (id == R.id.nav_third_layout) { 
     fragmentManager.beginTransaction().replace 
       (R.id.content_main_container, new thirdFragment()).commit(); 
    } else if (id == R.id.nav_fourth_layout) { 
     fragmentManager.beginTransaction().replace 
       (R.id.content_main_container, new fourthFragment()).commit(); 
    } else if (id == R.id.nav_fifth_layout) { 
     fragmentManager.beginTransaction().replace 
       (R.id.content_main_container, new fifthFragment()).commit(); 
    } else if (id == R.id.nav_sixth_layout) { 
     fragmentManager.beginTransaction().replace 
       (R.id.content_main_container, new sixthFragment()).commit(); 
    } else if (id == R.id.nav_seventh_layout) { 
     fragmentManager.beginTransaction().replace 
       (R.id.content_main_container, new seventhFragment()).commit(); 
    } else if (id == R.id.nav_eighth_layout) { 
     fragmentManager.beginTransaction().replace 
       (R.id.content_main_container, new eighthFragment()).commit(); 
    } else if (id == R.id.nav_ninth_layout) { 
     fragmentManager.beginTransaction().replace 
       (R.id.content_main_container, new ninthFragment()).commit(); 
    } 

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

這裏是我的content_main_container 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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="mcshreker.pocketmath.MainActivity"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:srcCompat="@drawable/e" 
      android:id="@+id/imageView7" 
      android:layout_marginTop="96dp" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:contentDescription="" 
      tools:ignore="ContentDescription" /> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:srcCompat="@drawable/welcome" 
      android:id="@+id/imageView2" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      tools:ignore="ContentDescription" /> 
    </LinearLayout> 

</RelativeLayout> 

這裏是我的Activity_main 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"> 

    <include 
     layout="@layout/app_bar_main" 
     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_main" 
     app:menu="@menu/activity_main_drawer" /> 

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

幫助G非常感謝!

+0

你可以分享你的'xml'?你確定元素「content_main_container」是你的'activity_main'中的一個子元素嗎? – Paras

+0

我已經共享這兩個XML文件,如果有幫助。 – CodeLife

回答

2
No view found for id 0x7f0d00aa (mcshreker.pocketmath:id/content_main_container) 

檢查,如果content_main_containeractivity_main佈局定義

+0

除了我的content_main_container不在我的activity_main內,它們是兩個不同的XML文件。 – CodeLife

+0

您需要將此佈局(容器)放入活動佈局中。片段將被放置在這個容器中。 – Manza

+0

好了,所以這消除了錯誤(謝謝),但現在當我點擊其中一個片段時,窗口(片段佈局)不會加載,所以我每次都卡在同一個窗口中。此外,還有一堆滯後。我不擔心與片段加載一樣多的延遲。 – CodeLife

相關問題