2017-08-24 63 views
-2

這是抽屜佈局,我搜索了很多次來解決問題,但每次我我停在同一點...所以任何人可以請幫助我,我用的,而不是預先設計的抽屜佈局Android Studio中的空白布局創建抽屜佈局java.lang.IllegalArgumentException:沒有找到ID爲0x7f0e0084(com.example.dell.foodcourt:id/content)的片段HomeFragment

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

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello"/> 
</LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigationmenu" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     app:menu="@menu/menudetails" 
     android:layout_gravity="start"> 
    </android.support.design.widget.NavigationView> 


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

//這是爲家庭片段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:id="@+id/homeFragment" 
    tools:context="com.example.dell.foodcourt.HomeFragment"> 

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

//抽屜佈局Java類

package com.example.dell.foodcourt; 

import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
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.view.MenuItem; 

import static android.app.PendingIntent.getActivity; 

/** 
* Created by DELL on 19-08-2017. 
*/ 

public class Userhome extends AppCompatActivity { 
    private DrawerLayout drawerLayout; 
    private ActionBarDrawerToggle toggle; 
    private NavigationView navigationview; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.user_homepage); 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout); 
    toggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close); 
    navigationview= (NavigationView) findViewById(R.id.navigationmenu); 
    drawerLayout.addDrawerListener(toggle); 
    toggle.syncState(); 
    setupDrawerContent(navigationview); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

} 

private void setupDrawerContent(NavigationView navigationView){ 
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){ 
     @Override 
     public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
      selectDraweritem(item); 
      return true; 
     } 
    }); 
} 

//我在這裏主要面臨的問題,到了我的知識,所以任何人可以幫我解決這個問題

public void selectDraweritem(MenuItem menuItem) { 
    Fragment fragment=null; 
switch (menuItem.getItemId()){ 
    case R.id.homeFragment: 
     fragment=new HomeFragment(); 
     break; 
    case R.id.navigation_Profile: 
     fragment=new Profile_Fragment(); 
     break; 
    default: 
     fragment=new HomeFragment(); 
     break; 
} 
if (fragment!=null){ 
    FragmentManager fragmentManager=getSupportFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.content,fragment).commit(); 
} 
DrawerLayout drawerLayout= (DrawerLayout) findViewById(R.id.drawerlayout); 
    drawerLayout.closeDrawer(GravityCompat.START); 
    } 


public boolean onOptionsItemSelected(MenuItem item){ 
    if(toggle.onOptionsItemSelected(item)){ 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

} 

//首頁片段Java類是在這裏

package com.example.dell.foodcourt; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import java.util.zip.Inflater; 


/** 
* A simple {@link Fragment} subclass. 
*/ 
public class HomeFragment extends Fragment { 


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


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     inflater.inflate(R.layout.fragment_home,container,false); 
     if(savedInstanceState==null){ 
      getFragmentManager().beginTransaction().replace(R.id.content,new HomeFragment()).commit(); 
     } 
     return super.onCreateView(inflater,container,savedInstanceState); 
    } 

} 

回答

0

在您的HomeFragment中,您應該返回視圖以在onCreateView方法生命週期中膨脹,例如:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView = inflater.inflate(R.layout.fragment_home,container,false); 
     // This part should be in the activity who handle the fragment 
     /*if(savedInstanceState==null){ 
      getFragmentManager().beginTransaction().replace(R.id.content,new HomeFragment()).commit(); 
     }*/ 
     return rootView; 
    } 

而且在目的上顯示,你可以在你的活動佈局中添加的FrameLayout作爲片段容器中的片段:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawerlayout"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello"/> 
</LinearLayout> 

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

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigationmenu" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     app:menu="@menu/menudetails" 
     android:layout_gravity="start"> 
    </android.support.design.widget.NavigationView> 
</android.support.v4.widget.DrawerLayout> 

希望這有助於

+0

真的很感謝你們cochi,你只是解決了我的3天問題,在10分鐘...帽子天才... – satish

+0

歡迎您:)請考慮通過點擊複選標記接受答案。這表明你已經找到了解決方案。沒有義務這樣做。 – Cochi

+0

Cochi還有一個問題,當我點擊菜單項中的項目時,它正在移動到片段,但沒有顯示與此相關的家庭或個人資料相關的片段信息 – satish

相關問題