Java Progres对话框使异步任务中的活动崩溃
我有一个正在工作的异步任务,其中放置了一个进度对话框。问题是,当我将“进度”对话框放入其中时,它会崩溃:Java Progres对话框使异步任务中的活动崩溃,java,android,android-asynctask,progressdialog,Java,Android,Android Asynctask,Progressdialog,我有一个正在工作的异步任务,其中放置了一个进度对话框。问题是,当我将“进度”对话框放入其中时,它会崩溃: public class CheckBeerJSON extends AsyncTask <String, Void, String> { Context c; String b; private ProgressDialog Dialog = new ProgressDialog(c); public CheckBeerJSON(Contex
public class CheckBeerJSON extends AsyncTask
<String, Void, String> {
Context c;
String b;
private ProgressDialog Dialog = new ProgressDialog(c);
public CheckBeerJSON(Context context, String beer)
{
c = context;
b = beer;
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
protected void onPreExecute() {
Dialog.setMessage("Checking your portfolio");
Dialog.setTitle("Searching");
Dialog.show();
}
protected void onPostExecute(String result){
//decode json here
try{
JSONObject json = new JSONObject(result);
String status = json.getString("status");
if(status.equals("no")){
String message = "Beer not logged";
Toast.makeText(c, message, Toast.LENGTH_SHORT).show();
//clear loader image
LinearLayout ll = (LinearLayout) (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
ll.removeAllViews();
//Add beer add button
LayoutInflater mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout addButton = (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
addButton.addView(mInflater.inflate(R.layout.addbeerbutton_layout, null));
}
else{
String message = "You have the beer!!";
Toast.makeText(c, message, Toast.LENGTH_SHORT).show();
//clear loader image
LinearLayout ll = (LinearLayout) (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
ll.removeAllViews();
//inflate star rater
LayoutInflater mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout addButton = (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
addButton.addView(mInflater.inflate(R.layout.addrate_layout, null));
RatingBar r = (RatingBar) ((Activity) c).findViewById(R.id.beerRatingBar);
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
String userID = prefs.getString("userID", null);
//get beer rating with async task and update rate bar
String url = "my_other_url";
String userURLComp = "u=" + userID;
String beerID = "&b=" + b;
url = url + userURLComp + beerID;
new GetUserRating(c,r).execute(url);
//add listener to bar
addListenerOnRatingBar(c);
}
}
catch(Exception e){
}
Dialog.dismiss();
}
private void addListenerOnRatingBar(Context view) {
RatingBar ratingBar = (RatingBar) ((Activity) view).findViewById(R.id.beerRatingBar);
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
//next async task to update online database
float stars = ratingBar.getRating();
//get user details
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
String userID = prefs.getString("userID", null);
//get beer id
String beerID = b;
//get rating
String urlRate = "r=" + String.valueOf(ratingBar.getRating());
String urlUserID = "&u=" + userID;
String urlBeerID = "&b=" + beerID;
//construct url
String url2 = "my_url";
url2 = url2 + urlRate + urlUserID + urlBeerID;
Log.d("addRateing", url2);
//async task to update rating in database
new UpdateRating(c).execute(url2);
}
});
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
}
公共类CheckBeerJSON扩展异步任务
{
上下文c;
b串;
private ProgressDialog=新建ProgressDialog(c);
公共CheckBeerJSON(上下文,字符串beer)
{
c=上下文;
b=啤酒;
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
返回readJSONFeed(arg0[0]);
}
受保护的void onPreExecute(){
setMessage(“检查你的投资组合”);
对话框.setTitle(“搜索”);
Dialog.show();
}
受保护的void onPostExecute(字符串结果){
//在这里解码json
试一试{
JSONObject json=新的JSONObject(结果);
String status=json.getString(“status”);
如果(状态等于(“否”)){
字符串消息=“啤酒未记录”;
Toast.makeText(c,message,Toast.LENGTH_SHORT).show();
//清除加载程序映像
LinearLayout ll=(LinearLayout)(LinearLayout)((活动)c).findViewById(R.id.addBeerLayout);
ll.removeAllViews();
//添加啤酒添加按钮
LayoutInflater mInflater=(LayoutInflater)c.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
LinearLayout addButton=(LinearLayout)((活动)c).findViewById(R.id.addBeerLayout);
addButton.addView(mInflater.充气(R.layout.addbeerbutton_layout,null));
}
否则{
String message=“你有啤酒!!”;
Toast.makeText(c,message,Toast.LENGTH_SHORT).show();
//清除加载程序映像
LinearLayout ll=(LinearLayout)(LinearLayout)((活动)c).findViewById(R.id.addBeerLayout);
ll.removeAllViews();
//充气式星级评定器
LayoutInflater mInflater=(LayoutInflater)c.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
LinearLayout addButton=(LinearLayout)((活动)c).findViewById(R.id.addBeerLayout);
addButton.addView(mInflater.inflate(R.layout.addrate_layout,null));
RatingBar r=(RatingBar)((活动)c).findViewById(r.id.beerRatingBar);
//获取用户数据
SharedPreferences=PreferenceManager.getDefaultSharedPreferences(c);
字符串userID=prefs.getString(“userID”,null);
//使用异步任务和更新速率栏获取啤酒评级
String url=“我的其他url”;
字符串userURLComp=“u=”+userID;
字符串beerID=“&b=“+b;
url=url+userURLComp+beerID;
新建GetUserRating(c,r).execute(url);
//将侦听器添加到工具栏
addListenerOnRatingBar(c);
}
}
捕获(例外e){
}
Dialog.dismise();
}
私有void addListenerOnRatingBar(上下文视图){
RatingBar RatingBar=(RatingBar)((活动)视图);
setOnRatingBarChangeListener(新的OnRatingBarChangeListener(){
公共无效评级已更改(评级杆、浮动评级、,
布尔值(用户){
//更新联机数据库的下一个异步任务
浮星=ratingBar.getRating();
//获取用户详细信息
SharedPreferences=PreferenceManager.getDefaultSharedPreferences(c);
字符串userID=prefs.getString(“userID”,null);
//拿到啤酒证
字符串beerID=b;
//获得评级
String urlRate=“r=”+String.valueOf(ratingBar.getRating());
字符串urlUserID=“&u=“+userID;
字符串urlBeerID=“&b=“+beerID;
//构造url
字符串url2=“我的url”;
url2=url2+urlRate+urlUserID+urlBeerID;
Log.d(“添加”,url2);
//在数据库中更新分级的异步任务
新的更新(c.execute)(url2);
}
});
}
公共字符串readJSONFeed(字符串URL){
StringBuilder StringBuilder=新的StringBuilder();
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(URL);
试一试{
HttpResponse response=httpClient.execute(httpGet);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
如果(状态代码==200){
HttpEntity=response.getEntity();
InputStream InputStream=entity.getContent();
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(inputStream));
弦线;
而((line=reader.readLine())!=null){
stringBuilder.append(行);
}
inputStream.close();
}否则{
Log.d(“JSON”,“未能下载文件”);
}
}捕获(例外e){
d(“readJSONFeed”,例如getLocalizedMessage());
}
返回stringBuilder.toString();
}
}
我得到的错误是:
08-16 21:22:34.054 10772-10772/com.beerportfolio.beerportfoliopro E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beerportfolio.beerportfoliopro/com.example.beerportfoliopro.BeerPage}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.access$600(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5536)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
at android.app.AlertDialog.<init>(AlertDialog.java:98)
at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
at com.example.beerportfoliopro.CheckBeerJSON.<init>(CheckBeerJSON.java:37)
at com.example.beerportfoliopro.BeerPage.onCreate(BeerPage.java:83)
at android.app.Activity.performCreate(Activity.java:5066)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
... 11 more
08-16 21:22:34.054 10772-10772/com.beerportfolio.beerportfolio E/AndroidRuntime:致命异常:main
java.lang.RuntimeException:无法启动活动组件信息{com.beerportfolio.beerportfolio/com.example.beerportfoliopro.BeerPage}:java.lang.NullPointerException
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
在android.app.ActivityThread.access上每小时600美元
private ProgressDialog Dialog;
public CheckBeerJSON(Context context, String beer)
{
c = context;
Dialog = new ProgressDialog(c);
b = beer;
}