0

我想捕获这个错误,以便我可以自己处理它而不向用户显示它。我该怎么做???

我正在使用适用于 Windows Azure 的 Android 官方 SDK。

此错误仅出现 2-3% 的时间。所有其他时间它连接良好。

此外,如果我尝试联系 azure 的活动不再存在,则上下文不再存在,它会尝试在不存在的上下文中显示消息,这将使应用程序崩溃。

此代码基本上直接来自 windows azure 的 todolist 示例。

谢谢,

安东尼·G。

        try {
            // Create the Mobile Service Client instance, using the provided
            // Mobile Service URL and key
            mClient = new MobileServiceClient(
                    "XXXXXXX",
                    "YYYYYYYYY", this)
                    .withFilter(new ProgressFilter());

            createTable();

        } catch (MalformedURLException e) {
            createAndShowDialog(
                    new Exception(
                            "There was an error creating the Mobile Service. Verify the URL"),
                    "Error");
        } catch (Exception e) {
            Log.d(TAG, "Exepction caught" + e.toString());
        }

这是表创建部分。

try {

        Log.d(TAG, "Create table called and started. ");

        // Get the Mobile Service Table instance to use
        // Don't use the default, because the table on Azure has a different name than the class, instead use this call. 
        mToDoTable = mClient.getTable("MY_TABLE",
                MY_TABLE_SPECIAL_CLASS.class);

        // Create an adapter to bind the items with the view
        mAdapter = new DownloadedMapsListAdapter(this, R.layout.row_list_show_maps_to_download);
        ListView listViewToDo = (ListView) findViewById(R.id.listview_data_fromAzure);

        //listViewToDo.setOnItemClickListener(mMessageClickedHandler); 
        listViewToDo.setAdapter(mAdapter);          

        // Load the items from the Mobile Service
        refreshItemsFromTable();

    } catch (Exception e) {
        Log.d(TAG, "Exepction caught" + e.toString());
    }

在此处输入图像描述

4

1 回答 1

0

好吧,我是个白痴。在 refreshItemsFromTable() 中有实际的“执行”语句被调用(我没有在我的问题中发布它)。执行是它实际联系 Azure 的时间。该函数检查异常是否为空,而不是使用 try-catch。所以显示的是 CreateAndShowDialog(exception, string)。

也许这会帮助别人。

/**
 * Refresh the list with the items in the Mobile Service Table
 */
private void refreshItemsFromTable() {

    // Get the items that weren't marked as completed and add them in the
    // adapter

    Log.d(TAG, "refreshItemsFromTable");
    mToDoTable.execute(new TableQueryCallback<MapObjects_FromAzure>() {

        public void onCompleted(List<MapObjects_FromAzure> result, int count,
                Exception exception, ServiceFilterResponse response) {
            if (exception == null) {

                Log.d(TAG,
                        "refreshItemsFromTable on complete, with no exception thrown so far. ");

                mAdapter.clear();

                for (MapObjects_FromAzure item : result) {
                    mAdapter.add(item);

                }

            } else {
                createAndShowDialog(exception,
                        "Error" + exception.toString());
            }
        }
    });
于 2013-08-30T01:23:09.350 回答