0

我想從一個JSON文件中加載一個圖像,點擊從該文件加載到活動中的列表項。顯示圖像及其描述。從一個活動的片段中加載圖像FileNotFoundException錯誤

它加載活動並顯示圖像,但不是描述。 Logcat中有nthing。

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

我是新手,這是我的第一個應用程序。如果你給我一個解決方案來解決這個問題,我會很高興。

回答

0

您在east.java中傳遞NULL作爲上下文

+0

我糾正了這一點,它加載了該活動,但未顯示傳遞的文本和圖像。我用新的Logcat和更新的代碼更新了問題。 THX – alireza 2015-04-08 20:08:36

+0

我改變了服務器中的圖像方向,它現在加載圖像。但它仍然沒有顯示它的描述。 – alireza 2015-04-08 20:30:32

相關問題