如何将主要活动发送到Fragment java类内部的类?

时间:2015-06-10 01:04:31

标签: android android-fragments android-activity nullpointerexception inner-classes

我得到了一个gridview,我用ASyncTask" inner"填充项目。类。我的内部类的父类正在扩展片段。当然,这个片段在主要活动中用于刷页面。我必须将活动发送到progressDialog和其他东西的内部类。我尝试了一些这样的话但是,每次我在logcat中得到NullPointerException错误。请帮帮我们。所有Fragment类代码都在下面;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentB extends Fragment {



    public FragmentB() {
        // Required empty public constructor
    }

    private ProgressDialog pDialog;
    private String url = "http://**********.com/api.asp?cmd=resdok&gore=tarih&kategori=all";

    //JSON NODLAR
    public static final String POST_ID = "id";
    public static final String FOTOYOL = "img";

    ArrayList<HashMap<String, String>> WallPaperList = new ArrayList<HashMap<String, String>>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_b, container, false);


        if (isConnected()){
            **// HERE IMPORTANT I THINK !**
            new ProgressTask(**v.getContext()**).execute();
        }
        else
        {
            Toast.makeText(v.getContext(),"Please check your internet connection..!",Toast.LENGTH_LONG).show();
        }

        /*
        GridView g = (GridView)v.findViewById(R.id.gridview);
        g.setAdapter(new AdapterB(v.getContext(),WallPaperList));
        */

        return v;
    }

    public boolean isConnected(){
        ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Activity.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected())
            return true;
        else
            return false;
    }


    private class ProgressTask extends AsyncTask<String,Void,Boolean>{
        private ProgressDialog progressDialog;
        private Context mContext;
        final AdapterB adapter = new AdapterB(mContext,WallPaperList);


        public ProgressTask(Activity activity)
        {
            **//AND HERE !!**
            this.mContext = activity;
            progressDialog = new ProgressDialog(mContext);
        }

        protected void onPreExecute() {
            this.progressDialog.setMessage("Please Wait");
            this.progressDialog.setCancelable(false);
            this.progressDialog.show();
        }

        @Override
        protected void onPostExecute(final Boolean success)
        {
            if (progressDialog.isShowing())
            {
                progressDialog.dismiss();
            }
        }

        protected Boolean doInBackground(final String... args)
        {
            JSONParser jsonParser = new JSONParser();
            JSONArray json  = jsonParser.getJSONArrayFromUrl(url);
            for (int i = 0;i < json.length(); i++)
            {
                try
                {
                    JSONObject c = json.getJSONObject(i);
                    String image = c.getString(FOTOYOL);
                    String id = c.getString(POST_ID);

                    HashMap<String,String> map = new HashMap<String,String>();
                    map.put(POST_ID, id);
                    map.put(FOTOYOL, image);

                    WallPaperList.add(map);
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }

            }
            return null;
        }

    }
}

0 个答案:

没有答案
相关问题