进度条错误

时间:2014-08-17 10:18:09

标签: android android-asynctask android-progressbar android-json

我想使用JSON解析数据并显示进度条。这个代码工作正常而没有进度条但是当我使用进度条时它的不幸就停止了!!!

这是我的MainActivity.java代码

public class MainActivity extends Activity {
private ProgressDialog pDialog;

//URL to get JSON Array
private static String url = "https://dl.dropboxusercontent.com/u/131328281/admission.json";

//JSON Node Names 
private static final String TAG_ADMISSION = "admission";
private static final String TAG_DATE = "date";
private static final String TAG_TITLE = "title";
private static final String TAG_GENINFO = "geninfo";
private static final String TAG_GENINFO2 = "geninfo2";
private static final String TAG_APPINCLUDE = "appinclude";
private static final String TAG_APPINCLUDE2 = "appinclude2";
private static final String TAG_UNDERGRAD = "undergrad";
private static final String TAG_UNDERGRAD2 = "undergrad2";
private static final String TAG_POSTGRAD = "postgrad";
private static final String TAG_POSTGRAD2 = "postgrad2";
private static final String TAG_ADMSN = "admsn";
private static final String TAG_ADMSN2 = "admsn2";
JSONArray admission = null;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.adm);
    new DownloadTask().execute();
}
private class DownloadTask extends AsyncTask<String, Void, Void> {



    /* Start the progress in onPreExecute */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         pDialog = new ProgressDialog(getApplicationContext()); 
            pDialog.setMessage("Loading ...Please Wait..");
            pDialog.setIndeterminate(false);
            pDialog.setCanceledOnTouchOutside(false);
            pDialog.setCancelable(true);
            pDialog.show();
    }
    protected Void doInBackground(String... params) {
    // Creating new JSON Parser
    JSONParser jParser = new JSONParser();

    // Getting JSON from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting JSON Array
        admission = json.getJSONArray(TAG_ADMISSION);
        JSONObject c = admission.getJSONObject(0);

        // Storing  JSON item in a Variable
        String date = c.getString(TAG_DATE);
        String title = c.getString(TAG_TITLE);
        String geninfo = c.getString(TAG_GENINFO);
        String geninfo2 = c.getString(TAG_GENINFO2);
        String appinclude = c.getString(TAG_APPINCLUDE);
        String appinclude2 = c.getString(TAG_APPINCLUDE2);
        String undergrad = c.getString(TAG_UNDERGRAD);
        String undergrad2 = c.getString(TAG_UNDERGRAD2);
        String postgrad = c.getString(TAG_POSTGRAD);
        String postgrad2 = c.getString(TAG_POSTGRAD2);
        String admsn = c.getString(TAG_ADMSN);
        String admsn2 = c.getString(TAG_ADMSN2);

        //Importing TextView
        final TextView name2 = (TextView)findViewById(R.id.geninfo);
        final TextView name3 = (TextView)findViewById(R.id.geninfo2);
        final TextView name4 = (TextView)findViewById(R.id.app_include);
        final TextView name5 = (TextView)findViewById(R.id.app_include2);
        final TextView name6 = (TextView)findViewById(R.id.undergrad);
        final TextView name7 = (TextView)findViewById(R.id.undergrad2);
        final TextView name8 = (TextView)findViewById(R.id.postgrad);
        final TextView name9 = (TextView)findViewById(R.id.postgrad2);
        final TextView name10 = (TextView)findViewById(R.id.admsn);
        final TextView name11= (TextView)findViewById(R.id.admsn2);

        final Button btn = (Button)findViewById(R.id.button1);

        //Set JSON Data in TextView
        btn.setText(title + "\n" + date);
        name2.setText(geninfo);
        name3.setText(geninfo2);
        name4.setText(appinclude);
        name5.setText(appinclude2);
        name6.setText(undergrad);
        name7.setText(undergrad2);
        name8.setText(postgrad);
        name9.setText(postgrad2);
        name10.setText(admsn);
        name11.setText(admsn2);



} catch (JSONException e) {
    e.printStackTrace();
}
return null;
}
        protected void onPostExecute(Void Void) {
            pDialog.dismiss();
        }
  }

这是adm.xml

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/color_buttons"
    android:textColor="#0059b2"
    android:textSize="20dp"
    android:textStyle="bold"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"/>
<TextView
    android:id="@+id/geninfo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="100dp"
    android:layout_below="@+id/Button1"
    android:textSize="15dp"
    android:textStyle="bold"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/geninfo2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/geninfo"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/app_include"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/geninfo2"
    android:textStyle="bold"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/app_include2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/app_include"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/undergrad"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:layout_below="@+id/app_include2"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/undergrad2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/undergrad"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/postgrad"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:layout_below="@+id/undergrad2"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/postgrad2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/postgrad"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/admsn"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:layout_below="@+id/postgrad2"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/admsn2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/admsn"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

这是JSONPaser.java

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

}

1 个答案:

答案 0 :(得分:0)

不是使用getApplicationContext for Progress对话框,而是尝试使用Activity上下文,这应该可以解决您的问题。