我想拆分我的downloadTask類並創建了一個新的類文件,但是當我嘗試在MainAcvtivity中調用該類時,我無法解析downloadTask();無法解析調用MainActivity之外的Android類的符號
public class downloadTask extends AsyncTask <String ,Void,String>{
public String desURL;
public String Date;
Context context;
private downloadTask(String desURL, String Date,Context context){
this.desURL = desURL;
this.Date = Date;
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
}
@Override
protected String doInBackground(String... args){
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//this method will be running on UI thread
}
}
請幫助解決問題,並在MainActivity中使用我的downloadTask類。
這裏是我的MainActivity.class
public class MainActivity extends AppCompatActivity {
Context context = this;
SharedPreferences sharedPref = context.getSharedPreferences(
getString(R.string.last_update_key), Context.MODE_PRIVATE);
String lastUpdate = sharedPref.getString(getString(R.string.last_update_key),"0");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if(lastUpdate == "0"){
Toast.makeText(this,R.string.need_for_update,Toast.LENGTH_LONG).show();
}
}
添加您的主要活動代碼或嘗試新的downloadTask()。exucute(); – Pavya
@Pavya沒有解決問題! –
我看不到任何'downloadTask'類相關的代碼在mainacitivity加上,'context'爲空 –