我有一个主活动类,我从中调用两个异步任务来加载外部(来自url)数据。我想显示进度条或简单的Toast消息,但我无法检索应用程序上下文。
这是异步类代码:
public class URLDataReader extends AsyncTask<URL, Void, String> {
@Override
protected void onPreExecute() {
// here where I need the context
}
String result = null;
@Override
protected String doInBackground(URL... params) {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(
params[0].openStream()
)
);
result = reader.readLine();
}catch(IOException e){
e.printStackTrace();
}
return result;
}
}
这是家庭活动类代码:
public class Home extends ActionBarActivity{
Context context = getApplicationContext();
public class Post{
String id;
String title;
String description;
String release;
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getRelease() {
return release;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setRelease(String release) {
this.release = release;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
String result = null;
ArrayList<Post> focusOn = new ArrayList<Post>();
try {
URL address = new URL("http://www.youth-stories.com/api/all.php");
URLDataReader reader = new URLDataReader();
result = reader.execute(address).get();
//Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
}catch (IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
} catch (ExecutionException e){
e.printStackTrace();
}
try {
JSONObject obj = new JSONObject(result);
String success = (String) obj.getString("success");
JSONArray records = obj.getJSONArray("records");
for(int i = 0; i < records.length(); i++) {
Post tmp = new Post();
tmp.setId(records.getJSONObject(i).getString("id"));
tmp.setTitle(records.getJSONObject(i).getString("title"));
tmp.setDescription(records.getJSONObject(i).getString("contents"));
tmp.setRelease(records.getJSONObject(i).getString("data_post"));
focusOn.add(tmp);
}
}catch (JSONException e){
e.printStackTrace();
}
//wrapper
LinearLayout container = (LinearLayout)findViewById(R.id.wrapper);
for(int i = 0; i < focusOn.size(); i++){
//item
LinearLayout item = new LinearLayout(getApplicationContext());
container.addView(item);
item.setOrientation(LinearLayout.HORIZONTAL);
//image
Bitmap imageHandler = null;
URL address = null;
try {
address = new URL("http://www.youth-stories.com/public/admin/CH_FocusOn/images/"+focusOn.get(i).getId()+"_thumb2.jpg");
URLImageReader reader = new URLImageReader();
imageHandler = reader.execute(address).get();
}catch(MalformedURLException e){
e.printStackTrace();
}catch (ExecutionException e){
e.printStackTrace();
}catch(InterruptedException e){
e.printStackTrace();
}
//Toast.makeText(getApplicationContext(),address.toString(),Toast.LENGTH_LONG).show();
ImageView asset = new ImageView(getApplicationContext());
//asset.setImageResource(R.drawable.ic_launcher);
asset.setImageBitmap(imageHandler);
item.addView(asset);
LinearLayout.LayoutParams imgSettings = new LinearLayout.LayoutParams(300,300);
asset.setLayoutParams(imgSettings);
//inside
LinearLayout contents = new LinearLayout(getApplicationContext());
contents.setOrientation(LinearLayout.VERTICAL);
item.addView(contents);
//title
TextView title = new TextView(getApplicationContext());
title.setText(focusOn.get(i).getTitle());
title.setTextAppearance(this, R.style.title);
contents.addView(title);
//description
TextView description = new TextView(getApplicationContext());
description.setText(focusOn.get(i).getDescription());
description.setTextAppearance(this, R.style.description);
contents.addView(description);
//date
TextView date = new TextView(getApplicationContext());
date.setText(focusOn.get(i).getRelease());
date.setTextAppearance(this, R.style.description);
contents.addView(date);
}
}
}
现在正确传递了上下文,我可以编写一个简单的Toast但是当我尝试显示进度对话框应用程序崩溃时。这是修改后的代码:
public class URLImageReader extends AsyncTask<URL, Void, Bitmap> {
Context context = null;
ProgressDialog dialog = null;
public URLImageReader(Context context){
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setMessage("Loading, please wait.");
dialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected Bitmap doInBackground(URL... params) {
Bitmap image = null;
try {
URL url= params[0];
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e){
e.printStackTrace();
}
return image;
}
}
答案 0 :(得分:1)
我想显示进度条或简单的Toast消息,但我想 无法检索应用程序上下文
使用URLDataReader
类构造函数获取Context以显示Toast和ProgressBar:
1。在URLDataReader
类中创建一个以Context为参数的构造函数:
private Context mContext;
public URLDataReader(Context mContext){
this.mContext=mContext;
}
2. 来自Home
活动创建URLDataReader
类的对象:
URLDataReader reader = new URLDataReader(Home.this);
现在在mContext
上使用onPreExecute
来显示进度条。
注意:强>
下面
imageHandler = reader.execute(address).get();
调用get()
方法停止执行当前线程,直到doInBackground
方法执行未完成。
imageHandler = reader.execute(address);
如果要显示进度条,只需调用执行方法并使用get()
处理onPostExecute
方法响应
doInBackground
方法
答案 1 :(得分:0)
首先,在context
内分配onCreate()
,如:
`context = this;`
并为constructor
context
定义asynctask
class
,然后将其从您调用的地方传递
`URLDataReader reader = new URLDataReader(context);`
并在URLDataReader.java
Context contextAsynctask;
public URLDataReader(Context context)
{
this.contextAsynctask = context;
}
忘记在context
班级
Asynctask
的部分
答案 2 :(得分:0)
在URLDataReader的构造函数中发送上下文,以便在Home类的onCreate方法中
URL address = new URL("http://www.youth-stories.com/api/all.php");
URLDataReader reader = new URLDataReader(context);
result = reader.execute(address).get();
然后在URLDataReader类
中void URLDataReader (Context _context) {
this.context = _context; // declare a global variable called context in your URLDataReader class
}