繁体   English   中英

android应用的弹出错误消息

[英]popup error message for android app

我是android应用程序开发的新手,这是我的第一个应用程序。 我正在尝试从用户那里获取一些数据,然后将其发送到我的php后端Web服务。

但是,我想启用异常处理并在发生某种错误时出现错误弹出消息。

我的signup.java文件中目前有以下代码

//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.
        }

    }

我的signup.xml表单如下所示

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

我需要对sigupform.xml文件和signup.java文件的相应代码添加哪些修改或元素,以实现所需的解决方案?

使用android AlertBuilder类的简单弹出对话框

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

代码源链接

要么

使用Toast简单反馈消息来提醒用户

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

这是我过去的工作方式:

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

当我感到兴奋时:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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