在活动FileNotFoundException错误中从片段加载图像

时间:2015-04-01 17:32:59

标签: java android android-intent android-fragments nullpointerexception

我想在点击从此文件加载到活动中的列表项后,从JSON文件加载图像。显示图像及其描述。

它加载活动并显示图像,但不显示它的描述。 Logcat中有一些东西。

east.java

@SuppressLint("NewApi")  
public class east extends Fragment {

    ListView list;
    TextView id;
    TextView name;
    Button Btngetdata;
    east adaptor;

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



    //URL to get JSON Array
    private static String url = "http://ra.com/s/east.php";

    //JSON Node Names
    private static final String TAG_ARRAY = "coffee";
    private static final String TAG_ID = "ID";
    private static final String TAG_NAME = "name";
    private static final String TAG_adress = "adress";
    private static final String TAG_IMAGE = "image";

    JSONArray coffee = null;

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

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        oslist = new ArrayList<HashMap<String, String>>();
        Btngetdata = (Button)getView().findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new JSONParse().execute();  
                Btngetdata.setVisibility(View.GONE);
            }
        });

    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {

        private ProgressDialog pDialog;
        JsonParser jParser = new JsonParser();
        private JSONObject json;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            name = (TextView)getView().findViewById(R.id.name);
            id = (TextView)getView().findViewById(R.id.id);

            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();


        }

        @Override
        protected JSONObject doInBackground(String... args) {

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

            return json;
        }



        @Override
        protected void onPostExecute(JSONObject json) {

            pDialog.dismiss();
            try {
                 // Getting JSON Array from URL
                 coffee = json.getJSONArray(TAG_ARRAY);

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

                     JSONObject c = coffee.getJSONObject(i);

                     // Storing  JSON item in a Variable
                     String ID = null;
                     String name = null;
                     String adress = null;
                     String image = null;
                     try {
                        ID = new String(c.getString(TAG_ID).getBytes("ISO-8859-1"), "utf8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                        name = new String(c.getString(TAG_NAME).getBytes("ISO-8859-1"), "utf8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                        adress = new String(c.getString(TAG_adress).getBytes("ISO-8859-1"), "utf8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                            image = new String(c.getString(TAG_IMAGE).getBytes("ISO-8859-1"), "utf8");
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                     // Adding value HashMap key => value
                     HashMap<String, String> map = new HashMap<String, String>();
                     map.put(TAG_ID, ID);
                     map.put(TAG_NAME, name);
                     map.put(TAG_adress, adress);
                     map.put(TAG_IMAGE, image);

                     oslist.add(map);

                     list = (ListView)getView().findViewById(R.id.listView1);

                     ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                         R.layout.listview,
                         new String[] { TAG_ID, TAG_NAME, TAG_adress }, new int[] {
                             R.id.id, R.id.name, R.id.adress});
                     list.setAdapter(adapter);
                     list.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                        @Override
                         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(getActivity(), SingleItemView.class); 
                                // Pass all data rank
                                intent.putExtra("rank", oslist.get(+position).get("TAG_ID"));
                                // Pass all data country
                                intent.putExtra("country", oslist.get(+position).get("TAG_NAME"));
                                // Pass all data population
                                intent.putExtra("population", oslist.get(+position).get("TAG_adress"));
                                // Pass all data flag
                                intent.putExtra("flag", oslist.get(+position).get("image"));
                                // Start SingleItemView Class
                                startActivity(intent);
                         }
                     });
                     }
            } catch (JSONException e) {
              e.printStackTrace();
            }
        }
    }
}

SingleItemView.java

public class SingleItemView extends Activity {
// Declare Variables
String rank;
String country;
String population;
String flag;
String position;
ImageLoader imageLoader = new ImageLoader(this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from singleitemview.xml
    setContentView(R.layout.singleitemview);

    Intent i = getIntent();
    // Get the result of rank
    rank = i.getStringExtra("rank");
    // Get the result of country
    country = i.getStringExtra("country");
    // Get the result of population
    population = i.getStringExtra("population");
    // Get the result of flag
    flag = i.getStringExtra("flag");

    // Locate the TextViews in singleitemview.xml
    TextView txtrank = (TextView) findViewById(R.id.rank);
    TextView txtcountry = (TextView) findViewById(R.id.country);
    TextView txtpopulation = (TextView) findViewById(R.id.population);

    // Locate the ImageView in singleitemview.xml
    ImageView imgflag = (ImageView) findViewById(R.id.flag);

    // Set results to the TextViews
    txtrank.setText(rank);
    txtcountry.setText(country);
    txtpopulation.setText(population);

    int loader = R.drawable.loader;

    // Capture position and set results to the ImageView
    // Passes flag images URL into ImageLoader.class
    imageLoader.DisplayImage(flag, loader, imgflag);
}
}

我是新手,这是我的第一款应用。如果你给我一个解决方案来解决这个问题,我会很高兴的。

1 个答案:

答案 0 :(得分:0)

您在East.java中传递NULL作为上下文