Unfortunately App has stopped!

Hi all,

I am sorry if this questions has been asked in other threads or forums, but however I couldn't find any answer to this questions.
Let me explained my issue here.

"I build an apps using Xamarin for Android applications. The apps consists of 2 textfields which is Username and Password and a Button to access the mysql string connection to verify the Username and Password, if correct it will open a new form, if failed, a message will display."

A lot of threads or forums does give some advice such as Disable the Shared Runtime and Allow Internet Permissions.
As per above advice, it is done, but when I run the Apps, the error appear and app is closed.

But surprisingly, when I create other apps that does not require Internet Access, it works Great!
However, the apps which required access to Internet, give the above error "Unfortunately, App has stopped!"

Please kindly advice me and help me in this issue, else I have to learn Java for Android Studio T.T

Best Answers

Answers

  • AlbertKAlbertK MYMember ✭✭✭✭

    You need to set the permission for internet access.

    VS Menu -> AppName Properties -> Android Manifest -> Required Permisssion

    Tick the check box on "Internet".

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AlbertK

    I did it but still cannot resolve the issue.

  • AlbertKAlbertK MYMember ✭✭✭✭

    Can you run the app in debug mode and see which line of code it is crashing on?

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AlbertK

    May I know how to Run in Debug mode?

    Thanks.

  • AlbertKAlbertK MYMember ✭✭✭✭
    edited June 2017

    Change the the option marked in RED rectangle from the attached image to Debug.

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AlbertK

    Will update you the crashing code once I reach home, sorry for late reply.
    Thanks

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭
    Does the device / simulator you're using have internet access? (fire up chrome on it and try and google something) because if you try to connect to the internet, it can't and you don't catch the exception then yes, it'll probably crash
  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    I am sure that the device I am using has Internet Access, I do not use emulator as it not working on my PC.
    How do I catch the exception?

    Thanks

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭

    wrap a try / catch around the code block you think might be suspect, eg;

    try
            {            
                //your code in here
    
            }
    
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
    
  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AlbertK and @AndyFlisher

    Here is my code

    using Android.App;
    using Android.Widget;
    using Android.OS;
    using MySql.Data.MySqlClient;

    namespace App5
    {
    [Activity(Label = "App5", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
    private Button btnConnection;
    private TextView txtSystemLog;
    protected override void OnCreate(Bundle bundle)
    {
    base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            btnConnection = FindViewById<Button>(Resource.Id.XbtnConnection);
            txtSystemLog = FindViewById<TextView>(Resource.Id.XtxtSystemLog);
    
            btnConnection.Click += BtnConnection_Click;
    
        }
    
        private void BtnConnection_Click(object sender, System.EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection("Server=localhost;Port=3306;Initial Catalog=localhoost_db;User ID=admin;Password=admin");
            try
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                    txtSystemLog.Text = "Connection Successful";
                }
            }
            catch (MySqlException ex)
            {
                txtSystemLog.Text = ex.ToString();
            }
        }
    }
    

    }

    When I build an APK files, and run it, it shows me "Unfortunately, Apps has stopped"
    When I tried to run in emulator, it shows the emulator, but the program is not run at all.

    Please help me

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭

    Do your button and TextView exist in your Main Layout? What happens if you replace the Main Layout content with something simpler, is there a syntax problem with the XAML? Try doing a clean build (including deleting the obj and bin folders at file system level), And as per earlier advice, what happens if you run as a Debug build? Any messages

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    This is the debug I received

    [I:]: Starting Adb server (adb start-server)
    [I:]: Adb start-server operation completed
    [D:]: TrackDeviceTask got:
    [I:]: Got new device list from adb with 0 devices
    [D:]: TrackDeviceTask got: emulator-5554 offline
    [I:]: Got new device list from adb with 1 devices
    [D:]: TrackDeviceTask got: emulator-5554 device
    [I:]: Got new device list from adb with 1 devices
    [D:RunShellCommand]: emulator-5554 getprop
    [D:RunShellCommand]: emulator-5554 getprop
    [D:]: TrackDeviceTask got:
    [I:]: Got new device list from adb with 0 devices
    [D:]: TrackDeviceTask got: emulator-5554 offline
    [I:]: Got new device list from adb with 1 devices
    [D:]: TrackDeviceTask got: emulator-5554 device
    [I:]: Got new device list from adb with 1 devices
    [D:RunShellCommand]: emulator-5554 getprop
    [D:RunShellCommand]: emulator-5554 getprop
    [D:]: TrackDeviceTask got:
    [I:]: Got new device list from adb with 0 devices

    Thanks

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    Below is my Main.axml source

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



  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭

    The source didn't post, and that just looks like the adb server log, there should be an output window in Visual Studio or similar that has lots of debug out put as the app is running, you can use it crudely to do Console.Writeline messages periodically to see how far it's got

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    How can I get the debug window?
    I am using Visual Studio Community 2017

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭
    Try View -> windows -> output or similar when debugging (don't have a copy in front of me)
  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    I think this is the output

    View > Output > Debug
    Inspector extension loaded
    Build Failed: Xamarin.Android does not support running the previous version. Please ensure your solution builds before running or debugging it.

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭
    Aha, getting there, it sounds like there's an error and it's not building, you get a box saying 'do you want to run the previous version' am guessing you clicked yes? Well no point is that doesn't work. Look in the build output : logs for the errors, correct them then get a clean build - until then you won't see anything on the emulator
  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    Can you teach me how to correct it?
    I am quite a beginner in Xamarin.

    Thanks

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    This is the Build Output
    1>Starting deploy 7_WSVGA_Tablet_API_25 ...
    1>Starting emulator 7_WSVGA_Tablet_API_25 ...
    1>C:\PROGRA~2\Android\ANDROI~1\tools\emulator.EXE -partition-size 512 -no-boot-anim -avd 7_WSVGA_Tablet_API_25 -prop monodroid.avdname=7_WSVGA_Tablet_API_25
    1>PANIC: Cannot find AVD system path. Please define ANDROID_SDK_ROOT
    1>Emulator 7_WSVGA_Tablet_API_25 cannot be started.
    ========== Deploy: 0 succeeded, 1 failed, 0 skipped ==========

    What should I do next?

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭

    Need to know what the error is first, not sure how you've managed it but you've either closed or can't see all the windows that are telling you what's going on. There should be another window called error list (look in same place where you activated the build output window), the error list will show you all the build errors (normally in red), and tell you what line number and what there error is, double clicking the error should take you to that line of code.

    This is not a new to Xamarin thing though, this is just learning how to use visual studio

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    Here is the Image for Error List

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭

    The Android SDK version (25) is lower than the target framework version (26), at least that's how I'm interpreting that, right click the project and choose a lower target framework version

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    Thanks for the help, it solved the AndroidManifest.xml error.
    However these error still remain and I have no Idea how to get rid of it as it says attribute not declared.

  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭
    Can't really tell without seeing the xaml for your layout, but they are warnings which generally won't stop it building and I think most of those you can ignore although the one about the label jumps out, you probably have a syntax error in the xaml in and around the word label somewhere.

    Have a look, and then do a clean on the solution and try running it again
  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AndyFlisher

    This is my Main.axml Layout

    This is my Main.axml Source

    I tried to do a clean build by deleting the obj and bin in the System Path, but still the Apps closed when I run at my Android Device.

  • AlbertKAlbertK MYMember ✭✭✭✭

    When you say App close, Do you mean that the application close when you click on the "Check Connection" button?

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AlbertK

    Yes, you're right in saying!

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AlbertK

    May I know this line of code is placed where?
    catch (Exception ex1)
    {
    //Capture all other Errors
    txtSystemLog.Text = ex1.Message;
    }

    When I follow the code (Exception) is not recognized.
    If I remove the above Catch(Exception) line, and build the apk, when installing in my android, it says Installation Failed

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AlbertK

    I follow the code but it says namespace for "Exception" cannot be found, missing directories or assemblies

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AlbertK @AndyFlisher

    Yea, understand that localhost won't work in android phone, that was just an example as to protect my database.
    After following the guidelines, the Error is disappear when I press the button "Check Connection"

    But in the txtSysLog() TextView, it shows the following:
    "Encoding 1252 data could not be found. Make sure you have correct international codeset assembly installed and enabled."

    Any idea what is International codeset assembly?

  • CaptainSamuelCaptainSamuel MYMember ✭✭

    Hi @AlbertK and @AndyFlisher

    Thanks so much for the help! It works.
    Big thumb up for you both :smiley:

Sign In or Register to comment.