App works on emulator but not on device

giorgilekveishviligiorgilekveishvili GEMember ✭✭
edited December 2014 in Xamarin.Android

Hello, I was using some fragments in my project and i figured it out that when I save fragments and get them later in OnCreate() method it stops working on device but not on emulator please help!

`using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V13;

namespace Fragments_Sample
{
[Activity(Label = "Fragments_Sample", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity,IConnection
{
Fragment1 fragmenta { get; set; }
Fragment2 fragmentb { get; set; }

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        if (bundle == null)
        {
            fragmenta = new Fragment1();
            fragmentb = new Fragment2();

            FragmentTransaction tr = FragmentManager.BeginTransaction();
            tr.Add(Resource.Id.linearLayout1, fragmenta, "firstFragment");
            tr.Add(Resource.Id.linearLayout2, fragmentb, "secondFragment");

            tr.Commit();
        }
        else {
            fragmenta = FragmentManager.GetFragment(bundle, "first") as Fragment1;
            fragmentb = FragmentManager.GetFragment(bundle, "second") as Fragment2;

            FragmentTransaction tr = FragmentManager.BeginTransaction();
            tr.Add(Resource.Id.linearLayout1, fragmenta, "firstFragment");
            tr.Add(Resource.Id.linearLayout2, fragmentb, "secondFragment");

            tr.Commit();
        }


    }

    protected override void OnSaveInstanceState(Bundle outState)
    {
        base.OnSaveInstanceState(outState);

        FragmentManager.PutFragment(outState, "first", fragmenta);
        FragmentManager.PutFragment(outState, "second", fragmentb);
    }

    public void changeText(int num)
    {
        Fragment2 temp = FragmentManager.FindFragmentByTag("secondFragment") as Fragment2;
        temp.changeLabel(num);
    }
}



public interface IConnection {
    void changeText(int num);
}

}

`

Fragment1 class:

`
public class Fragment1 : Fragment
{
Button _btn;
IConnection _con;
int counter = 0;

    public override void OnAttach(Activity activity)
    {
        base.OnAttach(activity);

        _con = activity as IConnection;
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.Inflate(Resource.Layout.fragment_a,container,false);
    }

    public override void OnActivityCreated(Bundle savedInstanceState)
    {
        base.OnActivityCreated(savedInstanceState);

        _btn = this.Activity.FindViewById<Button>(Resource.Id.button1);
        _btn.Click += _btn_Click;

        if (savedInstanceState != null)
            counter = savedInstanceState.GetInt("counter");
    }

    public override void OnSaveInstanceState(Bundle outState)
    {
        base.OnSaveInstanceState(outState);
        outState.PutInt("counter", counter);
    }

    void _btn_Click(object sender, EventArgs e)
    {
        _con.changeText(++counter);
    }
}

`

Fragment2 Class

`
public class Fragment2 : Fragment
{
TextView _text;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.fragment_b, container, false);
}

    public override void OnActivityCreated(Bundle savedInstanceState)
    {
        base.OnActivityCreated(savedInstanceState);

        _text = Activity.FindViewById<TextView>(Resource.Id.textView1);
    }

    public void changeLabel(int n)
    {
        _text.Text = String.Format("თქვენ ღილაკს დააჭირეთ {0} ჯერ ", n);
    }
}

`

Posts

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Do you get any error message or exceptions that could help you and us to identify the problem?

  • I do not get any exception on emulator, but on device Unfortunately App Has Stopped I think FragmentManager.PutFragment() and FindFragmentByTag have problem. Thank You

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Place a breakpoint at the places you think the app crashes and see if it happens.

Sign In or Register to comment.