简体   繁体   中英

Xamarin Android - fragment controls not being shown in actvitiy

I am trying to call a new activity from a fragment. I have a button at the toolbar, and when it is pressed the following code is being called (this event is inside the fragment) :

 Intent secondactitvity = new Intent(this.Activity,typeof(Activity_SecondActivity));
 StartActivity(secondactitvity );

Now the new activity is being called and here is my code inside OnCreate of the new activity:

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);

Here is the 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>

Here is the layout for the fragment:

<?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>

And here is the fragment code:

 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);
    }

Now the problem is. The second activity remains empty, it is not being filled with the fragment (but there is not an error message). I would understand it if the fragment cannot be found or something like that, but in this case all the functions from the fragment ( onCreateView , onActivityCreated ) are being called, and the Toast is also being displayed, but the activity is empty. Then I tried to insert a text view at the top of the fragment inside the second activity layout file, and its being shown but, the controls from the fragment remain un-visible. Then I tried to change the fragment associated to the second activity, I changed it to the fragment which is calling the activity, and this one is being properly loaded (I changed it inside the .axml file).

What am I missing here, for me it seems like the second one is not being created property and thus the controls cannot be displayed (but, I may also be wrong)?

Thank You

well after spending far too much hours on this i got the solution. First thing i have noticed is that i cannot acces Views from the second fragment layout (when trying to read them in code i could not compile). Then i created a new axml file, assigned it as the layout for the second fragment, and the controls are being displayed. So for me it seems like a bug in Xamarin, somehow the first axml has not been registered accordingly (but funny thing is that i could access the axml per se, but not the controls).

I am somebody who is migrating from Java (Android development) to Xamarin, and i have put a lot of hope on it. But I must admit that after few weeks lots of the first enthusiasm is gone. There is a lot missing in Xamarin and as time is passing by I am more thinking about moving back to Java and start learning Objective C for iOS. This project which i am doing now in Xamarin, i would have done it already if I were using Java, but for now I am still in the early phase (of course it cannot be as fast as in Java because i have to google for everything, but ...). Thers is also the issue with the Resource file, sometimes when my project breaks I cannot stop it from executing and i have to kill Visual Studio via Taskbar. In most cases when I do this the Resource file gets "destroyed" and when I open the project again i cannot start it anymore (in most cases I didnt bother because I was practising and I created a new one, but what if it is a bigger project ?) The next thing is the missing IntelliSense inside the .axml when the designer is open (when I open it with the xml viewer i get the IntelliSense). Do i have to switch always between these views ? And now this thing with the fragment, how i am supposed to trust the system if such cruical things dont work right.

Since I am still in the trial period I will give this whole thing a chance, but as I sad, the "fun" working with it is decreasing ...

Sorry for the long post, but just had the need to say :)

I had the same problem and nothing I had done solved it, including removing the layout and adding it again even with different name.

I figured out that I had messed up the method and forgot the override keyword. After this fix, it worked just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM