gpt4 book ai didi

java - 如何在Android中使用doInBackground AsyncTask?

转载 作者:行者123 更新时间:2023-12-01 14:27:09 25 4
gpt4 key购买 nike

我已经在 Android 中实现了 ppt 文件转换,并且正在使用 REST 等 Web 服务。但我的代码在主线程上运行,因此我必须对 AsynchTask 使用 do-In-Background() 。

当我运行应用程序时,我收到如下错误/警告:

View Root CalledFromWrongThreadException only the original thread that created a view hierarchy

没有 do-In-Background() 可以正常运行,但在运行状态下按后退按钮时应用程序崩溃。这是我的代码。

      btnSubmit.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View arg0)
{

convertToImage();

}

});

protected void onActivityResult
(int requestCode, int resultCode, Intent data)


{
// TODO Auto-generated method stub

if (requestCode == PATH)

{
if (resultCode == RESULT_OK)

{
if (data != null)

{
path = data.getStringExtra("path");
System.out.println("Path : " + path );

String strfpath = path ;

String fileName2
=strfpath.substring(strfpath.lastIndexOf("/")+1);

System.out.println("fileName2 :" + fileName2 );

function_arg1.setText(fileName2);
}

}

}

}

这是我的 doInBackground() 方法

private void convertToImage() 
{
httpGetAsynchTask httpGetAsyncTask = new httpGetAsynchTask();
httpGetAsyncTask.execute();
}


class httpGetAsynchTask extends AsyncTask<String , Integer , String>
{
protected void onPreExecute() {
try{
dialog.setMessage("Please Wait");
dialog.show();
}
catch(Exception e)
{

}
}




@Override
protected String doInBackground(String... arg0)
{
// TODO Auto-generated method stub

if (function_arg1.getText().length() == 0)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(StorageFolderUploadFile.this);
dialog.setTitle("Error");
dialog.setMessage("Please Enter Require Fields");
dialog.setNeutralButton("Ok", null);
dialog.show();
}


else

{
fileName = function_arg1.getText().toString();
System.out.println("File Name :- " + fileName);

folderName="tazeen";

Folder obj = new Folder();

try
{
response = obj.uploadFile(path/*, folderName*/);
Log.e("",""+response);
if (response)

{
result.append("File Uploaded Successfully \n");

Document docObj=new Document( fileName /*folderName*/);
count = docObj.getSlideCount();


if (count > 0)
{
result.append(" \n Numbers Of Slides = " + count);
System.out.println("Number Of Slides " + count );



for(int i=1; i <= count ; i++)
{

System.out.println("________________________________________");

System.out.println("i :-> " + i );

String outputPath = fileName + "_Tazeen" + i + ".jpg" ;
System.out.println("outputPath :-> " + outputPath);

slideNumber = i;
System.out.println("slideNumber :-> " + slideNumber);

Document docObj2=new Document(fileName);
System.out.println("docObj2 :-> " + docObj2);

docObj2.saveSlideAs(outputPath.toString().trim(), slideNumber, imageFormat);
System.out.println("docObj2 :-> " + docObj2);

result.append("slide is converted and save to your sdcard \n");

String imagePath = path + "/" + outputPath ;
System.out.println("imagePath : " + imagePath );


}

}

}

else

{
result.append("Oops..Something went wrong");
}
}


catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();


}



}


return fileName;
}




@Override
public void onPostExecute(String values)
{

convertToImage();

try
{
if(dialog.isShowing())
{
dialog.dismiss();
}
}
catch(Exception e)
{}
}

}

最佳答案

您无法从后台线程访问与用户界面有关的任何内容。因此,所有 AlertDialog.Builder 对话框 都是不允许的,如果 function_arg1View 那么也应该避免这种情况。

注意:我担心 System.out 是否被允许,但根据评论应该没问题。

如果有帮助的话,当我使用AsyncTask时,我通常创建一个类来保存所有可能的结果,包括错误消息和抛出的任何异常。然后,该对象可以按照 onPostExecute 中的要求反射(reflect)到用户界面。

关于java - 如何在Android中使用doInBackground AsyncTask?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17122806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com