1

In my fragment getting error when i place my progress bar in ListView in fragment my app was crashed ,progress bars was not displaving when I load my fragment please nay one help me how to resolve this

Fragment.java

ListAdapter adapter;
public static ArrayList<Pojo> gridData;
 GridView grd;
 private ProgressBar mProgressBar;
private String Sam_URL = "http://example.com/rest/Main/document?name=kids@_$example";


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.gridlayout_progressbar, container, false);
    System.out.println("Servce Called");
    gridData=new ArrayList<Pojo>();

    grd =(GridView)rootView.findViewById(R.id.gridview);
    Async as=new Async(getActivity(),grd);
    as.execute(Sam_URL);

    mProgressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
   // grd.setBackgroundColor(Color.CYAN);
    grd.setVerticalSpacing(7);
    grd.setHorizontalSpacing(7);



   grd.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub

        Intent intent=new Intent(getActivity(),KidsWear.class);
        intent.putExtra("pos", position+"");
        startActivity(intent);

    }
});

    return rootView;
}







class Async extends AsyncTask<String, Void, Integer>{
    Context context;
    GridView gridView;


    public Async(Context context,GridView gridView) {
        // TODO Auto-generated constructor stub

        this.context=context;
        this.gridView=gridView;
    }



    @Override
    protected Integer doInBackground(String... params) {
        // TODO Auto-generated method stub


        Integer result = 0;
        try {
            // Create Apache HttpClient
            //HttpClient httpclient = new DefaultHttpClient();
            URL url = new URL(Sam_URL);
            URLConnection urlConnection = url.openConnection();
            InputStream in = new BufferedInputStream(
                    urlConnection.getInputStream());

            // int statusCode =
            // httpResponse.getStatusLine().getStatusCode();

            // 200 represents HTTP OK
            if (true) {
                String response = streamToString(in);
                parseResult(response);
                result = 1; // Successful
            } else {
                result = 0;       // "Failed
            }
        } catch (Exception e) {

        }

        return result;



    }
    String streamToString(InputStream stream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(stream));
        String line;
        String result = "";
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
        }

        // Close stream
        if (null != stream) {
            stream.close();
        }
        return result;
    }

    @Override
    protected void onPostExecute(Integer result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


        if (result == 1) {

    gridView.setAdapter(new MyAdapter(context,gridData));

    gridView.setVisibility(View.VISIBLE);



    }mProgressBar.setVisibility(View.GONE); 

    }   



    private void parseResult(String result) {
        try {

            Log.d("MainActivity", "JSON Result : " + result);
            JSONArray response = new JSONArray(result);

            for (int i = 0; i < response.length(); i++)

            {

                JSONObject obj = response.getJSONObject(i);

                String Doc_name = obj.getString("documentName");
                Log.d("documentName",Doc_name);

                String Doc_file = obj.getString("documentFile");
            String Doc_content = obj.getString("documentContent");

            String Doc_offer=obj.getString("offer");
            String Doc_address=obj.getString("address");

                //Log.d("documentName","JSON Result : " + result);

                Pojo gd = new Pojo();



                gd.setDocumentName(Doc_name);

                gd.setDocumentFile(Doc_file);
                gd.setOffer(Doc_offer);

                gd.setDocumentContent(Doc_content);
                gd.setAddress(Doc_address);

                gridData.add(gd);


            }


        } catch (JSONException e) {
            e.printStackTrace();
        }


        }
}

where below my log cat error please any one help me

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference

salih kallai
  • 877
  • 2
  • 11
  • 32
chanti
  • 561
  • 2
  • 7
  • 17

1 Answers1

3

You haven't initialized mProgressBar before using it.

Call Async task after initializing it.

mProgressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
Async as=new Async(getActivity(),grd);
as.execute(Sam_URL);
Rohit Arya
  • 6,458
  • 1
  • 21
  • 37