0

我收到异常“无法在函数 doFirstTime() 中未调用 Looper.prepare() 的线程内创建处理程序。我正在尝试使用 Dropbox API 在 Dropbox 上传我的数据你能告诉我如何解决吗?

public class DownloadFile extends AsyncTask<Void, Long, Boolean> 

`{

private Context mContext;
private DropboxAPI<?> mApi;
private String mPath;
private FileOutputStream mFos;
private String mErrorMsg;
private StringBuilder xmlcode,newXMLCode;
private final static String FILE_NAME = "fuelrecords.xml";
private final static String ZIP_FILE_NAME = "fuelpad.zip";
private String dropbox_xml_records[];
private ArrayList<ArrayList<String>> dropbox_records;
private ArrayList<ArrayList<String>> database_records;
private ExpenseOperations eop;
private UploadFile up;
private boolean no_file;

public DownloadFile(Context context, DropboxAPI<?> api,String dropboxPath) 
{
    // We set the context this way so we don't accidentally leak activities
    mContext = context.getApplicationContext();
    mApi = api;
    mPath = dropboxPath;
    dropbox_records = new ArrayList<ArrayList<String>>();
    database_records = new ArrayList<ArrayList<String>>();
    eop = new ExpenseOperations(mContext);
    xmlcode=new StringBuilder("");
    newXMLCode=new StringBuilder("");
    no_file = false;
}

@Override
protected Boolean doInBackground(Void... params)
{
    Log.d("yes1", " in do in back of download..");
    try 
    {

        // Get the metadata for a directory
        Entry dirent = mApi.metadata(mPath, 1000, null, true, null);

        if (!dirent.isDir || dirent.contents == null) 
        {
            // It's not a directory, or there's nothing in it
            mErrorMsg = "Could not locate the file...";
            return false;
        }

        String cachefilePath = mContext.getCacheDir().getAbsolutePath() + "/" + FILE_NAME;
        String cachezipPath = mContext.getCacheDir().getAbsolutePath() + "/" + ZIP_FILE_NAME;
        try 
        {
            mFos = new FileOutputStream(cachezipPath);
        } 
        catch (FileNotFoundException e) 
        {
            mErrorMsg = "Couldn't create a local file to store the image";
            return false;
        }
        Notification("SmartExpense", "Now syncing to dropbox");
        mApi.getFile("/SmartExpenses.zip",null,mFos,null);

        try
        {
            FileInputStream fin = new FileInputStream(cachezipPath); 
            ZipInputStream zin = new ZipInputStream(fin); 
            ZipEntry ze = null;
            if((ze = zin.getNextEntry()) != null)
            {
                Log.v("Decompress", "Unzipping " + ze.getName()); 
                if(ze.isDirectory()) 
                { 

                } 
                else
                {   
                    FileOutputStream fout = new FileOutputStream(cachefilePath); 
                    for (int c = zin.read(); c != -1; c = zin.read()) 
                    { 
                        fout.write(c); 
                    } 
                    zin.closeEntry(); 
                    fout.close();
                }
            }
            zin.close(); 

        }
        catch(Exception ee)
        {
            Log.d("In unzip:", ""+ee);
        }

        try 
        {

            FileInputStream fs =new FileInputStream(cachefilePath);
            byte buff[] =new byte[1024];

            while(fs.read(buff)>0)
            {
                xmlcode.append(new String(buff));
            }
            fs.close();

            Log.d("Hhhhhhhhhhhaaaaaaaaaaaaa : ",""+xmlcode);

            Looper.prepare();


            if(!(xmlcode.toString().contains("<expenserecord>")) && getDBRecords())
            {
                doFirstTime();
                Log.d("1","1");
            }
            else if((xmlcode.toString().contains("<expenserecord>")) && getDBRecords())
            {
                Log.d("2","2");
                makeDropboxRecordArray();
                performSync();
            }
            else if((xmlcode.toString().contains("<expenserecord>")) && !getDBRecords())
            {
                Log.d("3","3");
                makeDropboxRecordArray();
                fillDBwithDropboxRecords();
            }
            else if(!(xmlcode.toString().contains("<expenserecord>")) && !getDBRecords())
            {
                Log.d("4","4");
                mErrorMsg ="No records exist to sync";
            }

        } 
        catch (Exception e) 
        {
            Log.d("Exception in doback: ",""+e);
        }

        return true;

    } 
    catch (DropboxUnlinkedException e) 
    {
        mErrorMsg = "Error :Dropbox unliked";
        // The AuthSession wasn't properly authenticated or user unlinked.
    } 
    catch (DropboxPartialFileException e) 
    {
        // We canceled the operation
        mErrorMsg = "Download canceled";
    } 
    catch (DropboxServerException e) 
    {
        // Server-side exception.  These are examples of what could happen,
        // but we don't do anything special with them here.
        if (e.error == DropboxServerException._304_NOT_MODIFIED) 
        {
            mErrorMsg = "Server Error.....";
            // won't happen since we don't pass in revision with metadata
        } 
        else if (e.error == DropboxServerException._401_UNAUTHORIZED) 
        {
            mErrorMsg = "Server Error : Unautherized user...";
            // Unauthorized, so we should unlink them.  You may want to
            // automatically log the user out in this case.
        } 
        else if (e.error == DropboxServerException._403_FORBIDDEN) 
        {
            mErrorMsg = "Server Error : Access denied";
            // Not allowed to access this
        } 
        else if (e.error == DropboxServerException._404_NOT_FOUND) 
        {
            no_file = true;
            doFirstTime();
            // path not found
        } 
        else if (e.error == DropboxServerException._406_NOT_ACCEPTABLE) 
        {
            mErrorMsg = "Server Error : Congestion...";
            // too many entries to return
        } 
        else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) 
        {
            // user is over quota
            mErrorMsg = "Server Error : Insufficient Storage...";
        } 
        else 
        {
            // Something else
            mErrorMsg = "Server Error...";
        }
        // This gets the Dropbox error, translated into the user's language
        mErrorMsg = e.body.userError;
        if (mErrorMsg == null) 
        {
            mErrorMsg = e.body.error;
        }
    } 
    catch (DropboxIOException e) 
    {
        // Happens all the time, probably want to retry automatically.
        mErrorMsg = "Network error.  Try again.";
    } 
    catch (DropboxParseException e) 
    {
        // Probably due to Dropbox server restarting, should retry
        mErrorMsg = "Dropbox error.  Try again.";
    } 
    catch (DropboxException e) 
    {
        // Unknown error
        mErrorMsg = "Unknown error.  Try again.";
    }
    return false;
}

public void fillDBwithDropboxRecords()
{
    Log.d("In fill db","yetoy");
    try
    {


        for(int i=0 ; i<dropbox_records.size()-1 ; i++)
        {
            {
                eop.addRecord(Integer.parseInt(dropbox_records.get(i).get(0)), dropbox_records.get(i).get(1), dropbox_records.get(i).get(2), Integer.parseInt(dropbox_records.get(i).get(3)));
            }
        }
    }
    catch (Exception e) 
    {
        Log.d("In fill db", ""+e);
    }
}

private void doFirstTime()
{
    Log.d("yes2", " in do first time of download..");

    try
    {
        if(!getDBRecords())
        {   
            mErrorMsg = "No records exist to sync";
            return;
        }

        newXMLCode.append("<smartexpense>");

        for(int i=0 ; i<database_records.size() ; i++)
        {
            newXMLCode.append("<expenserecord>");

            newXMLCode.append("<c_id>"+database_records.get(i).get(0)+"</c_id>");
            newXMLCode.append("<title>"+database_records.get(i).get(1)+"</title>");
            newXMLCode.append("<date>"+database_records.get(i).get(2)+"</date>");
            newXMLCode.append("<amount>"+database_records.get(i).get(3)+"</amount>");

            newXMLCode.append("</expenserecord>");
        }//for

        newXMLCode.append("</smartexpense>");

        up = new UploadFile(mContext,mApi,newXMLCode.toString());
        up.execute();

    }
    catch(Exception e)
    {
        Log.d("Exception in doFirtstTime : ",""+e);
    }
}//doFirstTime

public void makeDropboxRecordArray()
{
    Log.d("yes3", " in make record array of download..");
    try
    {
        dropbox_xml_records = (xmlcode.toString()).split("</expenserecord>");

        for(int i=0 ; i< dropbox_xml_records.length ; i++)
        {
            dropbox_records.add(new ArrayList<String>());

            dropbox_records.get(i).add(dropbox_xml_records[i].substring(
                ((dropbox_xml_records[i].indexOf("<c_id>"))+
                ("<c_id>".length())),
                dropbox_xml_records[i].indexOf("</c_id>")
            ));

            dropbox_records.get(i).add(dropbox_xml_records[i].substring(
                ((dropbox_xml_records[i].indexOf("<title>"))+
                ("<title>".length())),
                dropbox_xml_records[i].indexOf("</title>")
            ));

            dropbox_records.get(i).add(dropbox_xml_records[i].substring(
                ((dropbox_xml_records[i].indexOf("<date>"))+
                ("<date>".length())),
                dropbox_xml_records[i].indexOf("</date>")
            ));

            dropbox_records.get(i).add(dropbox_xml_records[i].substring(
                ((dropbox_xml_records[i].indexOf("<amount>"))+
                ("<amount>".length())),
                dropbox_xml_records[i].indexOf("</amount>")
            ));
        }


    }
    catch (Exception e) 
    {
        Toast.makeText(mContext,"In fill records :"+e , 2000).show();
    }
}

public boolean getDBRecords()
{
    Log.d("yes4", " in get dbrecords of download..");
    try
    {


        Cursor cc = eop.getRecords();
        if(cc.getCount() == 0)
            return false;

        int i=0;
        if(cc.moveToFirst())
        {
            do
            {
                database_records.add(new ArrayList<String>());
                database_records.get(i).add(cc.getString(cc.getColumnIndex("c_id")));
                database_records.get(i).add(cc.getString(cc.getColumnIndex("title")));
                database_records.get(i).add(cc.getString(cc.getColumnIndex("date")));
                database_records.get(i).add(cc.getString(cc.getColumnIndex("amount")));
                i++;

            }while(cc.moveToNext());
        }
        cc.close();


    }
    catch(Exception ee)
    {
        Toast.makeText(mContext,"getDBRecords :"+ee , 2000).show();
    }
    return true;
}

public void performSync()
{
    try 
    {
        //compare database records with dropbox records
        newXMLCode.append("<smartexpense>");

        for(int i=0 ; i<database_records.size() ; i++)
        {
            newXMLCode.append("<expenserecord>");

            newXMLCode.append("<c_id>"+database_records.get(i).get(0)+"</c_id>");
            newXMLCode.append("<title>"+database_records.get(i).get(1)+"</title>");
            newXMLCode.append("<date>"+database_records.get(i).get(2)+"</date>");
            newXMLCode.append("<amount>"+database_records.get(i).get(3)+"</amount>");

            newXMLCode.append("</expenserecord>");
        }


        for(int i=0 ; i<dropbox_records.size()-1 ; i++)
        {
                    eop.addRecord(Integer.parseInt(dropbox_records.get(i).get(0)), 
                            dropbox_records.get(i).get(1),
                            dropbox_records.get(i).get(2), 
                            Integer.parseInt(dropbox_records.get(i).get(3)));


                    newXMLCode.append("<expenserecord>");

                    newXMLCode.append("<c_id>"+dropbox_records.get(i).get(0)+"</c_id>");
                    newXMLCode.append("<title>"+dropbox_records.get(i).get(1)+"</title>");
                    newXMLCode.append("<date>"+dropbox_records.get(i).get(2)+"</date>");
                    newXMLCode.append("<amount>"+dropbox_records.get(i).get(3)+"</amount>");

                    newXMLCode.append("</expenserecord>");
                }

            //}

        newXMLCode.append("</smartexpense>");

        Log.d("Comming : ","yetoy..");

        up = new UploadFile(mContext,mApi,newXMLCode.toString());
        up.execute();
    } 
    catch (Exception e) 
    {
        Log.d("Perform sync:  ",""+e);
    }
}

@Override
protected void onPostExecute(Boolean result) 
{
    //mDbHelper.close();
    if (result)
    {
        //showToast("File successfully downloaded");
    } 
    else 
    {
        if(!no_file)
        {
            // Couldn't download it, so show an error
            showToast("Error in sync.Check notification.");
            Notification("SmartExpense", mErrorMsg);
        }
    }
}

private void showToast(String msg) 
{
    Toast error = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
    error.show();
}

// Notification Function
private void Notification(String notificationTitle, String notificationMessage)
{
    NotificationManager notificationManager = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(android.R.drawable.ic_menu_save, "Dropbox Sync", System.currentTimeMillis());

    Intent notificationIntent = new Intent(mContext, UploadFile.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);

    notification.setLatestEventInfo(mContext, notificationTitle, notificationMessage, pendingIntent);
    notificationManager.notify(10001, notification);
}

}

4

2 回答 2

1

当您尝试从后台线程更新 UI 时收到此错误。在你的情况下,doInBackground方法。

您似乎正在尝试doInBackground从以下行发布通知。

Notification("SmartExpense", "Now syncing to dropbox");

这可能是导致问题的原因。尝试对此以及您可能正在进行的任何其他 UI 更新发表评论doInBackground

于 2013-07-25T13:09:19.260 回答
0

Exception表明您正在尝试访问非 UI 线程中的 UI 元素。从您的代码中,问题可能是由您的方法中的这两行引起的doInBackground(您正在访问 Activity 的上下文):

String cachefilePath = mContext.getCacheDir().getAbsolutePath() + "/" + FILE_NAME;
String cachezipPath = mContext.getCacheDir().getAbsolutePath() + "/" + ZIP_FILE_NAME;

如果你在方法之外声明这两个变量doInBackgroud并在你的构造函数中实例化它们,你应该没问题。此外,请删除您正在调用的代码中的行,Looper.prepare()因为它们不会解决问题。

于 2013-07-25T13:01:59.197 回答