简体   繁体   中英

popup error message for android app

I am new to android app development and this is my first app. I am trying to have get some data from the user and then send it to my php backend webservice.

However I want to enable exception handling and have a error popup message when some kind of error occurs.

I currently have the following code in my signup.java file

//When the send button is clicked
    public void send(View v)
    {
        try{

            // CALL Validation method to make post method call
            validation();
        }
        catch(Exception ex)
        {
            // Error popup message to go with the error.
        }

    }

My signup.xml form looks like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/signup_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView6"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Please Register To Get Started"
        android:textSize="20sp" />

    .
    .
    .

    <Button
        android:id="@+id/signupbutton"
        style="@android:style/Widget.Material.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/phone"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="@drawable/rg_bg"
        android:text="Signup"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:onClick="send" />

</RelativeLayout>

What modification or elements do I need to add to the sigupform.xml file and the corresponding code for the signup.java file to implement the solution I am looking for?

Use android AlertBuilder class for simple popup dialog

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);


        // set title
        alertDialogBuilder.setTitle("Your Title");

        // set dialog message
        alertDialogBuilder
            .setMessage("Click yes to exit!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    MainActivity.this.finish();
                }
              })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

code source link

OR

Use Toast simple feedback messages to alert user

Toast toast = Toast.makeText(context, text, duration);
toast.show();

Here how i've done it in the past :

public class Tools  {
  public static void exceptionToast(Context context, String message) {
     Toast.makeText(context, message, Toast.LENGTH_LONG).show();
  }
}

and when i catch an excepetion :

Tools.exceptionToast(getApplicationContext(), e.getMessage());

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