English 中文(简体)
如何在强制关闭时保持活动?
原标题:How to keep activity on Force Close?

我有一段非常容易出错的代码,所以我用try{}catch语句包装了它。我不想回到上一个活动,我只想它停留在当前活动上,这样用户就可以编辑它们的任何错误。

我该如何实现?

    try{
        orgi.insertOrThrow(tableName, null, values);
        Toast.makeText(this, "You have successfully created a new profile!", 2).show();
        gotoProfileList();
        Log.e(getClass().getSimpleName(),"Successfully added to database");
    }catch(SQLiteException se){
        Log.e(getClass().getSimpleName(), "Database connection failed!" + se);

        //Stay in this activity...
    }finally{
        if (orgi != null){
            orgi.close();
        }
    }

算了吧,我可以通过显示一个alertDialog来解决我自己的问题,该对话框会告诉用户有关错误的信息。无论如何谢谢。:)

            try{
                orgi.insertOrThrow(tableName, null, values);
                Toast.makeText(this, "You have successfully created a new profile!", 2).show();
                gotoProfileList();
                Log.e(getClass().getSimpleName(),"Successfully added to database");
            }catch(SQLiteException se){
                Log.e(getClass().getSimpleName(), "Database connection failed!" + se);
                displayError();
                //stayInThisActivity();
            }finally{
                if (orgi != null){

                }   

public void displayError(){
            AlertDialog.Builder error = new AlertDialog.Builder(this);
            error.setMessage("That profile name already exists, try another one.").setCancelable(false).setPositiveButton("Yes",new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();

                }
            });

            AlertDialog alert = error.create();
            alert.setTitle("Error");
            alert.show();
        }
问题回答

Force closes are caused by uncaught exceptions. You only catche the SQLiteException in your example. You need to catch other Exceptions and handle them gracefully. Now a classical cause for FCs are use of null objects and resulting NullPointerExceptions. You shouldn t catch these. Your application will be too messed up to continue correctly in many cases. You can read more here: Catch_NullPointerException

在任何情况下,您都应该在模拟器中或连接的手机上运行该应用程序,导致崩溃,然后使用DDMS或“adb-logcat”登录到设备日志中。查看回溯,找到错误,修复它。如果你的应用程序在市场上,市场会列出你的用户设备为你发送的回溯。如果您(还没有?)使用Android Market,您可以让您的应用程序通过运行在web服务器上的PHP脚本远程记录堆叠比赛,并使用安卓远程堆栈竞赛

顺便说一句,最好将Exception作为第三个参数传递给Log方法,而不是将第二个参数连接到异常(从而隐式调用toString())。在您的示例中,这将是:

Log.e(getClass().getSimpleName(), "Database connection failed!", se);




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签