我想拆分我的downloadTask类并创建了一个新的类文件,但是当我尝试在MainAcvtivity中调用该类时,我得到了无法解析的downloadTask();
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();
}
}
答案 0 :(得分:1)
您有一个private
构造函数,因此无法从其他类创建此类的对象,因此请将其public
/*private*/ public downloadTask(String desURL, String Date,Context context){
this.desURL = desURL;
this.Date = Date;
this.context = context;
}
提示:移至onCreate
内的代码下方,因为context
将为空
sharedPref = getSharedPreferences(
getString(R.string.last_update_key), Context.MODE_PRIVATE);
lastUpdate = sharedPref.getString(getString(R.string.last_update_key),"0");
答案 1 :(得分:0)
我错误地在java(Test)部分制作了downloadTask文件,移动后我的问题解决了