簡體   English   中英

從活動FileNotFoundException錯誤中的片段加載圖像

[英]loading image from fragment in an activity FileNotFoundException error

單擊從此文件加載到活動中的列表項后,我想從JSON文件加載圖像。 顯示圖像及其描述。

它加載活動並顯示圖像,但不顯示其描述。 Logcat中什么也沒有。

東爪哇

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

我是新手,這是我的第一個應用程序。 如果您能給我解決該問題的解決方案,我將非常高興。

您正在將NULL作為上下文傳遞給east.java

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM