【问题标题】:Exception handling in androidandroid中的异常处理
【发布时间】:2016-01-13 00:00:59
【问题描述】:

我需要在我的应用程序中将异常显示为 toast 消息,但以下代码不起作用,我该如何修改?

public void onClick(View v) {
        try {
            if (btn66 == v) {
                ContentValues value = new ContentValues();
                value.put(DBhelper.Amount, txtBudget.getText().toString());
                value.put(DBhelper.Description, txr.getText().toString());

                if (txtBudget.length() == 0) {
                    txtBudget.requestFocus();
                    txtBudget.setError("Field Cannot Be Empty");
                } else {

                    db = helper.getWritableDatabase();
                    db.insert(DBhelper.TABLE2, null, value);
                    db.close();
                    clearfield();
                    Toast.makeText(this, "Budget add Successfully", Toast.LENGTH_LONG).show();
                    fetchData2();
                }
            }
        }
        catch (Exception sce)
        {
            Toast.makeText(this, "Error duplicate", Toast.LENGTH_LONG).show();
            System.out.println(sce.getMessage());

        }
    }

10-14 23:41:21.637 14498-14498/com.example.username.weddingplanning E/SQLiteDatabase:插入名称时出错=d android.database.sqlite.SQLiteConstraintException:列名不唯一(代码 19) 在 android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native 方法) 在 android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:968) 在 android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788) 在 android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86) 在 android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1575) 在 android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1445) 在 com.example.username.weddingplanning.addcategory.onClick(addcategory.java:67) 在 android.view.View.performClick(View.java:4439) 在 android.view.View$PerformClick.run(View.java:18398) 在 android.os.Handler.handleCallback(Handler.java:725) 在 android.os.Handler.dispatchMessage(Handler.java:92) 在 android.os.Looper.loop(Looper.java:176) 在 android.app.ActivityThread.main(ActivityThread.java:5299) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 在 dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 你得到了什么?
  • 显示你得到的结果
  • 我在 toast 中没有收到任何错误消息,但在 logcat 中出现了异常错误详细信息。
  • 用你的问题添加 logcat
  • 请检查我更新的问题

标签: android exception android-toast


【解决方案1】:

您可以从 Toast 文档中看到您正在使用的构造函数:

public static Toast makeText (Context context, CharSequence text, int duration)

Parameters
    context     The context to use. Usually your Application or Activity object.
    text        The text to show. Can be formatted text.
    duration    How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

第一个参数是Context

您传入this 编辑:这可能不是正确的Context,可以从中构造Toast

您需要将ContextActivityApplication 传递给它才能正常工作。

【讨论】:

  • 我也试过这样,仍然没有给出异常吐司消息。
  • 成功时会显示​​“预算添加成功”消息,但异常时不会显示“错误重复”消息?
  • 是的,如果我也添加了重复值,它会显示成功消息“预算添加成功”。(即使显示成功消息,重复值也不会添加到数据库中)
  • 您尝试过 YourActivity.this 和 YourActivity.this.getApplicationContext()?
【解决方案2】:

您必须在 Toast 的第一个参数中传递 YourActivity.this

例子:

Toast.makeText(YourActivity.this, "Your Message", Toast.LENGTH_LONG).show(); 

【讨论】:

  • 我也试过这样,仍然没有给出异常 toast 消息,它给出了“成功添加”的成功消息。但它没有被添加到数据库中。
  • 我认为 catch 块不会显示 Toast 消息,因此您必须保留 truefalse 并且在 catch 块之后您可以根据需要显示 Toast。
【解决方案3】:

最好检查插入语句的结果代码。 insert 语句始终返回新插入行的行 ID,如果发生错误,则返回 -1。

【讨论】:

  • 那么int result = db.insert(DBhelper.TABLE2, null, value);
  • if (result == -1) { 显示错误 } else { 做你的工作}
猜你喜欢
  • 2014-03-12
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
相关资源
最近更新 更多