2017-03-01 33 views
0

我試圖用不同的替換容器中的片段。我在顯示的新片段中做了Toast,但是佈局沒有顯示。替換片段時不會顯示佈局

這裏就是我更換片段:

Bundle bundle = new Bundle(); 
      bundle.putString("title", textContentList.get(position).getTitle()); 
      bundle.putString("crated", textContentList.get(position).getCreated()); 
      bundle.putString("author", textContentList.get(position).getAuthor()); 
      bundle.putString("fullContent", textContentList.get(position).getFullContent()); 
      MessageDetailsFragment messageDetailsFragment = new MessageDetailsFragment(); 
      messageDetailsFragment.setArguments(bundle); 
      replaceFragment(messageDetailsFragment); 

public void replaceFragment(Fragment fragment){ 
    FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
    transaction.replace(R.id.container, fragment); 
    transaction.commit(); 
} 

這裏是我的片段佈局:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/text_container" 
android:orientation="vertical" 
android:layout_marginTop="70dp" 
android:background="?android:colorBackground" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 


<TextView 
    style="@style/headings" 
    android:text="Author" 
    android:layout_marginTop="10dp" 
    android:id="@+id/message_details_author"/> 

<TextView 
    style="@style/subTxt_messageDetails" 
    android:text="Receiver" 
    android:id="@+id/message_details_receiver"/> 

<View 
    android:layout_width="320dp" 
    android:layout_gravity="center" 
    android:layout_height="1dp" 
    style="@style/dividerLine" /> 

<TextView 
    style="@style/headings" 
    android:layout_marginTop="10dp" 
    android:text="Title" 
    android:id="@+id/message_details_title"/> 

<TextView 
    style="@style/subTxt_messageDetails" 
    android:text="Date" 
    android:id="@+id/message_details_date"/> 

<View 
    android:layout_width="320dp" 
    android:layout_gravity="center" 
    android:layout_height="1dp" 
    style="@style/dividerLine" /> 

<TextView 
    android:text="Full content" 
    style="@style/subTxt_messageDetails" 
    android:layout_marginTop="15dp" 
    android:id="@+id/message_details_full_content"/> 

<TextView 
    style="@style/subTxt_messageDetails" 
    android:text="Sent by SMS" 
    android:textSize="15sp" 
    android:layout_marginTop="15dp" 
    android:id="@+id/message_sent_by"/> 

</LinearLayout> 

,這裏是我的新片段:

public class MessageDetailsFragment extends Fragment { 

private String title, created, author, fullContent; 
private TextView textViewTitle, textViewDate, textViewAuthor, textViewFullContent; 
private Bundle bundle; 
private Context context; 

public MessageDetailsFragment(){ 

} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    bundle = this.getArguments(); 
    title = bundle.getString("title"); 
    created = bundle.getString("created"); 
    author = bundle.getString("author"); 
    fullContent = bundle.getString("fullContent"); 
    context = getActivity(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_message_details, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState){ 
    Toast.makeText(context, "ADDASD", Toast.LENGTH_SHORT).show(); 

    textViewTitle = (TextView) getView().findViewById(R.id.message_details_title); 
    textViewTitle.setText(title); 
    textViewDate = (TextView) getView().findViewById(R.id.message_details_date); 
    textViewDate.setText(created); 
    textViewAuthor = (TextView) getView().findViewById(R.id.message_details_author); 
    textViewAuthor.setText(author); 
    textViewFullContent = (TextView) getView().findViewById(R.id.message_details_full_content); 
    textViewFullContent.setText(fullContent); 

} 
} 
+0

您的佈局不完整。它沒有結束標記,並且正在引用它不包含的視圖。如imageView更正您的佈局,然後再試一次,如果您仍然有問題 – Kuffs

+0

謝謝。我刪除了不應該在那裏的視圖,並且由於某種原因,結束標記沒有顯示在這裏。我仍然有同樣的問題 – Andypandy

+0

此外,無關,但在你的'OnCreate',你分配'getActivity'到上下文變量。這將始終爲空,因爲直到生命週期的後期纔會設置該活動。無需保留對上下文的引用,您可以稍後在需要時調用getActivity,並在使用它之前對null執行檢查。您的活動可能會完成,但引用仍然會存在於已完成的活動中,因此片段在使用時可能會導致崩潰 – Kuffs

回答

0

感謝所有的答案。事實證明,我試圖將片段插入到我也在應用程序中使用的viewpager中。通過創建一個新的FrameLayout並插入它,我解決了我的問題。

0

你的樣品不可重複的,因爲它指的是風格等沒有給出,所以很難說肯定,所以一般來說......

要調試Fragment佈局,首先清除除父ViewGroup以外的所有內容。給它一個容易識別的顏色,這樣你就可以看到它實際上出現在你的應用程序中,當你啓動它。

如果出現,問題是您的佈局。如果不是,那很可能是你的代碼。

完成此操作後,您可以逐漸添加其他視圖。

e.g

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:background="@color/Red" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

</LinearLayout