使用toast从catch语句显示错误消息

时间:2011-11-04 23:36:22

标签: java android

我的Android应用程序中有以下catch语句,我想通过toast显示任何错误消息,我可以这样做吗?

catch (Exception e) 
{
    //Helper.displayExceptionMessage(this, e.getMessage());
    Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
    e.printStackTrace();
}
public static void displayExceptionMessage(Context context, String msg)
{
    Toast.makeText(context, msg , Toast.LENGTH_LONG).show();
}

我还试图制作一个帮助类来显示消息,但我不知道如何解决“帮助”。

4 个答案:

答案 0 :(得分:5)

谢谢你们,

e.getMessage()完成了这个伎俩,

我是这样做的,

{
    ...    
    catch (Exception e) 
    {       
        e.printStackTrace();
        displayExceptionMessage(e.getMessage());
    }       
} 

public void displayExceptionMessage(String msg)
{
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

这样我可以用它来测试整个活动我猜。我不知道这可能是矫枉过正!!!

干杯,

麦克

答案 1 :(得分:0)

您可以尝试以下内容:

catch (System.Exception e){

           // log the error to the android logs.
            Android.Util.Log.D("MEDIA_PLAYER", e.Message);
            Toast.MakeText(this, e.Message, Toast.LENGTH_SHORT).Show();
        }

// log the error to the android logs. Android.Util.Log.D("MEDIA_PLAYER", e.Message); Toast.MakeText(this, e.Message, Toast.LENGTH_SHORT).Show(); }

答案 2 :(得分:0)

你可以试试像这样的东西,

catch (Exception e)
        { 
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        }

答案 3 :(得分:0)

如果您希望自定义错误,此方法对我有用,希望对您有所帮助:

 getActivity().runOnUiThread(Runnable { Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show() })
相关问题