Fragment中空对象引用的进度条

时间:2016-03-12 19:32:38

标签: android listview android-progressbar

在我的片段中出现错误,当我将进度条放在 ListView 片段中时,我的应用程序崩溃了,当我加载片段时,进度条没有移位请不要一个人帮我解决这个问题< / p>

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();
        }


        }
}

在我的日志猫错误下面,请任何人帮助我

  

java.lang.NullPointerException:尝试调用虚方法'void   空对象上的android.widget.ProgressBar.setVisibility(int)'   参考

1 个答案:

答案 0 :(得分:3)

在使用之前,您尚未初始化mProgressBar

初始化后调用异步任务。

mProgressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
Async as=new Async(getActivity(),grd);
as.execute(Sam_URL);