2015-05-29 135 views
-1

我正在通用圖像加載程序庫的幫助下構建一個應用程序。我的主要活動從一些url加載圖像並在gridview中顯示它。我想在單獨的活動中展示完整的圖像。當我點擊任何圖像以在新的活動中打開它時,我的應用程序就會崩潰。On Item點擊gridview我的下一個活動沒有顯示

我無法理解UIL給出的示例代碼,它的冗長。所以,我只想做網格視圖活動。

這裏是我的代碼:

ImageListAdapter.java

public class ImageListAdapter extends BaseAdapter { 

    public String[] urls = { 

      "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg", 
      "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg", 
      "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg", 
      "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s1024/Antelope%252520Butte.jpg", 
      "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s1024/Antelope%252520Hallway.jpg", 
      "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s1024/Antelope%252520Walls.jpg", 
      "https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg", 
      "https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s1024/Backlit%252520Cloud.jpg", 
      "https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s1024/Bee%252520and%252520Flower.jpg", 
      "https://lh5.googleusercontent.com/-n7mdm7I7FGs/URqueT_BT-I/AAAAAAAAAbs/9MYmXlmpSAo/s1024/Bonzai%252520Rock%252520Sunset.jpg", 
      "https://lh6.googleusercontent.com/-4CN4X4t0M1k/URqufPozWzI/AAAAAAAAAbs/8wK41lg1KPs/s1024/Caterpillar.jpg", 
      "https://lh3.googleusercontent.com/-rrFnVC8xQEg/URqufdrLBaI/AAAAAAAAAbs/s69WYy_fl1E/s1024/Chess.jpg", 

    }; 

    private Context context; 
    private ImageLoader imageLoader; 

    public ImageListAdapter(Context context) { 
     this.context = context; 
     imageLoader = ImageLoader.getInstance(); 
    } 

    @Override 
    public int getCount() { 
     return urls.length; 
    } 

    @Override 
    public Object getItem(int arg0) { 
     return null; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup viewGroup) { 
     View v = convertView; 

     ViewHolder vh = null; 
     if (v == null) { 
      v = View.inflate(context, R.layout.single_item, null); 

      vh = new ViewHolder(); 
      vh.imageView = (ImageView) v.findViewById(R.id.imageView); 

      v.setTag(vh); 
     } 
     else { 
      vh = (ViewHolder)v.getTag(); 
     } 

     DisplayImageOptions options = new DisplayImageOptions.Builder() 
       .cacheOnDisc() 
       .build(); 

     imageLoader.displayImage(urls[position], vh.imageView, options); 

     return v; 
    } 

    private class ViewHolder { 
     ImageView imageView; 
    } 

} 

MainActivity.java(這裏的影像在GridView中顯示)

public class MainActivity extends ActionBarActivity { 

    private ListView listView; 
    private GridView gridView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.grid_layout); 

     // listView = (ListView)findViewById(R.id.gridView); 
     gridView = (GridView) findViewById(R.id.gridView); 
     gridView.setAdapter(new ImageListAdapter(this)); 



     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Intent i = new Intent(getApplicationContext(), FullImageActivity.class); 
       i.putExtra("id", position); 
       startActivity(i); 

      } 
     }); 

    } 

} 

FullImageActivity.java

public class FullImageActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_full_image); 

     Intent i = getIntent(); 
     int position = i.getExtras().getInt("id"); 
     ImageListAdapter imageAdapter = new ImageListAdapter(this); 
     ImageView imageView = (ImageView) 
       findViewById(R.id.full_image_view); 
     imageView.setImageResource(Integer.parseInt(imageAdapter.urls[position])); 
    } 

} 

當我運行android studio時,沒有顯示錯誤。請幫忙。

+1

堆棧跟蹤將約爲現在真棒權利.. – codeMagic

回答

0

您是否已將新的Activity添加到清單文件中?

添加此

<activity 
    android:name=".FullImageActivity " 
    android:label="FullImageActivity " /> 
+0

是的。我已經添加了。仍然得到部隊封閉。 –

0

你不需要使用getextras()

repalace此行

Intent i = getIntent(); 
int position = i.getExtras().getInt("id"); 

Intent i = getIntent(); 
int position = i.getIntExtra("id",-1); 

編輯: 你是試圖從互聯網加載圖像,你不能直接使用圖像資源。

URL ulrn = new URL(url); 
HttpURLConnection con = (HttpURLConnection) ulrn.openConnection(); 
InputStream iS = con.getInputStream(); 
Bitmap bmp = BitmapFactory.decodeStream(iS); 
image.setImageBitmap(bmp); 
+0

不,它不工作。 –

+0

檢查更新的答案 –

0

你的問題是,

FullImageActivity活動找不到ImageView的

從這一行圖像資源,

imageView.setImageResource(Integer.parseInt(imageAdapter.urls[position])); 

您試圖列圖像的URL轉換成整數,因此可能是您正面臨的數字格式異常。從給出的圖片網址

只是加載位圖,並嘗試爲位圖設置爲ImageView的。只有

的僞碼的理解:從崩潰

Bitmap imageBitmap = loadBitmapFormUrl(imageAdapter.urls[position]); // Your method which load image bitmap from given url (please use asynchronously) 
imageView.setImageBitmap(imageBitmap); // After receiving image bitmap set it to imageView