[英]Xamarin Android - fragment controls not being shown in actvitiy
我試圖從一個片段中調用一個新的活動。 我在工具欄上有一個按鈕,當按下它時,將調用以下代碼(此事件在片段內) :
Intent secondactitvity = new Intent(this.Activity,typeof(Activity_SecondActivity));
StartActivity(secondactitvity );
現在正在調用新活動,這是新活動的OnCreate
中的代碼:
RequestWindowFeature(WindowFeatures.ActionBar);
ActionBar.SetIcon(Resource.Drawable.icon_toolbar_back_new);
ActionBar.SetCustomView(Resource.Layout.ActionBar_Custom_Layout);
ActionBar.SetDisplayShowCustomEnabled(true);
base.OnCreate(bundle);
SetContentView(Resource.Layout.SecondActivity_Layout);
這是SecondActivity_Layout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<fragment
class="frag.Fragment_Second"
android:id="@+id/fragment_secondActivity"
android:paddingTop="10dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
這是片段的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="@string/secondFragment_name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:paddingLeft="10dp" />
</LinearLayout>
這是片段代碼:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.Fragment_ProjectDetails, container); //view;// base.OnCreateView(inflater, container, savedInstanceState);
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Toast.MakeText(this.Activity, "activity for second fragment created",ToastLength.Short).Show();
base.OnActivityCreated(savedInstanceState);
}
現在的問題是。 第二個活動保持為空,沒有被片段填充(但沒有錯誤消息)。 如果找不到片段或類似的東西,我會理解的,但是在這種情況下,片段中的所有函數( onCreateView
, onActivityCreated
)都被調用,並且Toast
也正在顯示,但活動為空。 然后,我嘗試在第二個活動布局文件內的片段頂部插入一個文本視圖,並顯示了該視圖,但是片段中的控件仍然不可見。 然后,我嘗試更改與第二個活動相關聯的片段,將其更改為調用該活動的片段,然后正確加載了該片段(我在.axml
文件中對其進行了.axml
)。
我在這里缺少什么,對我來說好像第二個屬性沒有被創建,因此控件無法顯示(但是,我也可能錯了)?
謝謝
在花了太多時間后,我得到了解決方案。 我注意到的第一件事是我無法從第二個片段布局訪問視圖(當試圖以代碼形式讀取它們時,我無法編譯)。 然后,我創建了一個新的axml文件,將其分配為第二個片段的布局,並顯示控件。 因此,對我來說,這似乎是Xamarin中的錯誤,因此第一個axml尚未進行相應的注冊(但有趣的是,我本身可以訪問axml,但不能訪問控件)。
我是一個從Java(Android開發)遷移到Xamarin的人,我對此寄予了很大希望。 但是我必須承認,幾個星期后,許多第一熱情就消失了。 Xamarin缺少很多東西,隨着時間的流逝,我更多地考慮回到Java並開始學習iOS的ObjectiveC。 我現在在Xamarin中進行的這個項目,如果我使用Java,我早就已經完成了,但是目前我仍處於早期階段(當然,它不能像Java中那樣快,因為我必須通過Google搜索所有內容,但...)。 這些也是資源文件的問題,有時當我的項目中斷時,我無法停止其執行,而不得不通過任務欄殺死Visual Studio。 在大多數情況下,當我這樣做時,資源文件會被“破壞”,而當我再次打開項目時,我將無法啟動它(在大多數情況下,我並沒有因為我在練習並且創建了一個新文件而煩惱,但是如果它是更大的項目嗎?)接下來的事情是打開設計器時(當我使用xml查看器打開它時,我得到了IntelliSense)。axml中缺少IntelliSense。 我是否必須始終在這些視圖之間切換? 而現在,這些東西都帶有碎片,如果這些關鍵問題無法正常工作,我應該如何信任系統。
由於我仍處於試用期,因此我將給整個事情一個機會,但令我難過的是,與其合作的“樂趣”正在減少……
抱歉,很長的帖子,但只需要說:)
我遇到了同樣的問題,沒有做任何事情解決它,包括刪除布局並再次添加它,甚至使用不同的名稱。
我發現自己弄亂了方法,卻忘記了override
關鍵字。 修復之后,它工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.