2016-11-22 51 views
2

this is the screenshot of overwritten screen of both fragments我已經搜查的網站上,但我無法找到解決辦法,這裏是我的代碼:片段沒有發現的ID在android中的視圖ID更改時,它顯示兩個屏幕上覆蓋?

public class MyLawyerFragment extends Fragment { 
ImageButton im1; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View v1 = inflater.inflate(R.layout.activity_mylawyer, 
      container, false); 
    im1 = (ImageButton) v1.findViewById(R.id.imageButton); 
    im1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Fragment fragment = new tasks(); 
      FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      fragmentTransaction.replace(R.id.drawer_layout, fragment); 
      fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.commit(); 

     } 
    }); 
    return v1; 
} 

}

這裏是我的下一個片段的代碼,而我想去從最後一個:

public class tasks extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    { 

     super.onCreateView(inflater, container, savedInstanceState); 
     final View rootview = inflater.inflate(R.layout.activity_addlawyer, container, false); 



    return rootview; 
}}} 

這裏是任務片段的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 

android:id="@+id/rel3" 
android:layout_marginTop="60dp" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="ADD LAWYER" 
    android:id="@+id/textView29" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<View 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:background="#a8a8a8" 
    android:layout_below="@+id/textView29" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 
<TextView 
    android:layout_marginLeft="18dp" 
    android:layout_marginTop="58dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:text="Email ID :" 
    android:id="@+id/textView30" 
    android:layout_marginStart="31dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<EditText 
    android:layout_marginTop="8dp" 
    android:layout_marginRight="18dp" 
    android:background="@drawable/rounded_border_edittext" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textEmailAddress" 
    android:ems="10" 
    android:id="@+id/editText31" 
    android:layout_below="@+id/textView30" 
    android:layout_alignLeft="@+id/textView30" 
    android:layout_alignStart="@+id/textView30" /> 

<Button 
    android:layout_marginLeft="15dp" 
    android:background="@drawable/mybuttonred" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:text="CANCEL" 
    android:textColor="#ffffff" 
    android:id="@+id/button6" 
    android:layout_gravity="right" 
    android:layout_alignTop="@+id/button5" 
    android:layout_alignRight="@+id/editText32" 
    android:layout_alignEnd="@+id/editText32" 
    android:layout_toEndOf="@+id/button5" 
    android:layout_toRightOf="@+id/button5" /> 

<Button 
    android:layout_marginLeft="18dp" 
    android:background="@drawable/mybuttongreen" 
    android:layout_width="160dp" 
    android:layout_height="wrap_content" 
    android:text="SAVE LAWYER DETAILS" 
    android:textColor="#ffffff" 
    android:id="@+id/button5" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:layout_marginTop="29dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:text="Phone Number :" 
    android:id="@+id/textView31" 
    android:layout_below="@+id/editText31" 
    android:layout_alignLeft="@+id/editText31" 
    android:layout_alignStart="@+id/editText31" /> 

<EditText 
    android:layout_marginTop="8dp" 
    android:layout_marginRight="18dp" 
    android:background="@drawable/rounded_border_edittext" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textEmailAddress" 
    android:ems="10" 
    android:id="@+id/editText32" 
    android:layout_below="@+id/textView31" 
    android:layout_alignLeft="@+id/textView31" 
    android:layout_alignStart="@+id/textView31" /> 

現在我有這個錯誤的logcat:

Process: com.orion.android.mylawyerdiary, PID: 5453 
java.lang.IllegalArgumentException: No view found for id 0x7f0d006c (com.orion.android.mylawyerdiary:id/rel3) for fragment tasks{22ba0560 #1 id=0x7f0d006c} 

而且我無法解決它...請人幫助我。

這是所有片段連接我的導航抽屜活動的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:theme="@style/NavigationDrawerStyle" 
    android:background="#2B292A" 
    app:itemTextColor="#ffffff" 
    app:itemIconTint="#C09E57" 
    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/activity_navdrawer_header" 
    app:menu="@menu/menu_navdrawer" /> 

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

清理您的項目並重建它 –

+0

'rel3' id是否出現在'activity_mylawyer' .xml中? –

+0

@R.Zagórski不,rel3 id只存在於addlawyer.xml – Annie

回答

1

我改變了線按編輯,並通過添加此

if (container != null) { 
     container.removeAllViews(); 
    } 

修改addlawyer片段的onCreate方法並且它除去所有其他視圖,以便僅示出了其片段,其我選擇。

2

你的任務類中刪除此行super.onCreateView(inflater, container, savedInstanceState);

因爲如果你調用super.onCreateView方法,將爲你的片段視圖返回null。那條線以下的那條線不會被調用。

編輯: 好吧,我想你有contain_main.xml文件 更新此行fragmentTransaction.replace(R.id.rel3, fragment);fragmentTransaction.replace(R.id.content_container, fragment)

+0

@Linh Nguyen我從tasks.java中刪除了這一行,但不幸的是它沒有工作,並且我得到了與之前相同的錯誤。 – Annie

+0

fragmentTransaction.replace(R.id.rel3,fragment);也是錯誤的,你的視圖容器的片段是什麼?請發佈您的活動 –

+0

我想從MyLawyerFragment去任務片段,我已經發布了他們的代碼有問題。你能告訴我一個有效的方法,從一個片段到另一個片段,而不會產生空視圖錯誤。 – Annie

0

當更換片段如下代碼調用:

fragmentTransaction.replace(R.id.rel3, fragment); 

這意味着的是, Activity的視圖由ID rel3標識,應替換爲fragment

裏面.xml視圖的Acitivty(傳遞給setContentView方法)應該有與ID rel3將由fragment被替換的視圖。

+0

那麼如何創建這個視圖?對不起,我其實是個新手! – Annie

+0

https://developer.android.com/guide/components/fragments.html#Adding –

+0

我無法理解:(它很混亂 – Annie

相關問題