我在Android中实现了ppt文件转换,而我正在使用REST等Web服务。但是我的代码在主线程上运行,因此我必须使用do-In-Background()来执行AsynchTask。
当我运行应用程序时,我收到错误/警告:
查看Root CalledFromWrongThreadException仅显示创建视图层次结构的原始线程
没有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)
{}
}
}
答案 0 :(得分:1)
您无法从后台线程访问与用户界面有关的任何内容。因此,不允许所有AlertDialog.Builder dialog
,如果function_arg1
是View
,则应该避免这样做。
注意:我担心是否允许System.out
,但根据评论应该没问题。
如果它有用,当我使用AsyncTask
时,我通常创建一个类来保存所有可能的结果,包括错误消息和抛出的任何异常。然后,该对象可以根据onPostExecute
中的要求反映到用户界面。
答案 1 :(得分:0)
致电
new Async_Task(context, true).execute(multipartEntity, class);
public class Async_Task extends AsyncTask<Object,Boolean,String> {
HttpHelper parsing;
Object objs;
private ProgressDialog Dialog;
Context context;
Boolean flag=true;
public Async_Task(Context context,Boolean flag) {
this.context=context;
this.flag=flag;
parsing=new HttpHelper(context);
}
@Override
protected void onPreExecute() {
if(flag)
{
Dialog = new ProgressDialog(context);
Dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Dialog.setMessage("Please Wait...");
Dialog.setCancelable(false);
Dialog.show();
}
}
@Override
protected String doInBackground(Object... urls) {
MultipartEntity nameValuePairs=(MultipartEntity) urls[0];
objs=urls[1];
String obj=parsing.getJSONFromUrl(Constant.API,nameValuePairs);
// Log.i("output",obj);
return obj;
}
@Override
protected void onPostExecute(String result)
{
if(flag)
Dialog.dismiss();
if(objs instanceof LoginActivity)
{
((LoginActivity) objs).loginMessage(result);
}
}
}
答案 2 :(得分:0)
您应该避免Alert Dialog
内的Progress Dialog
,Toast
,doInBackground
等用户界面互动。因为AsyncTask
是一个后台进程,有助于减少活动主线程的负载。
您可以在启动AsyncTask
课程或onPreExecute
方法之前调用这些类型的对话框和祝酒词。