2016-03-20 94 views
0

如何解决此问题?MainActivity不包含类

MainActivity.this不是封闭类。

谢谢

public class uploadToServer extends AsyncTask<Void, Void, String> { 

protected void onPreExecute() { 
    super.onPreExecute(); 
    ProgressDialog pd= new ProgressDialog(MainActivity.this); //error is here 
    pd.setMessage("Wait image uploading!"); 
    pd.show(); 
} 
} 
+1

只需复制编码一个将它粘贴到'MainActivity'中。 –

+0

您必须将此类写入'MainActivity'或将'context'引用到该类中,并将其传递给'ProgressDialog' – ELITE

+0

您得到了答案吗?或问题没有解决啊 –

回答

2

AsyncTask构造,通过Context &使用它,当你从MainActivity这样需要

public class uploadToServer extends AsyncTask<Void, Void, String> { 
    private Context mContext; 
    public uploadToServer (Context context){ 
     mContext = context; 
    } 

    protected void onPreExecute() { 
     super.onPreExecute(); 
     ProgressDialog pd= new ProgressDialog(mContext); //change is here 
     pd.setMessage("Wait image uploading!"); 
     pd.show(); 
    } 
} 

呼叫,

uploadToServer task = new uploadToServer(getApplicationContext()); 
task.execute();