2016-05-14 65 views
-2

我編寫了這個代碼,使用畢加索從服務器下載圖像。以下是ImageAdapter.java:我不斷收到E/AndroidRuntime:致命例外:主

import com.squareup.picasso.Picasso; 
import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 
    int imageTotal = 7; 
    public static String[] mThumbIds = { 
      "http://imageshack.com/a/img923/2796/PB1lxo.jpg" 
    }; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return imageTotal; 
    } 

    @Override 
    public String getItem(int position) { 
     return mThumbIds[position]; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(480, 480)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 
     String url = getItem(position); 
     Picasso.with(mContext) 
       .load(url) 
       .placeholder(R.drawable.loader) 
       .fit() 
       .centerCrop().into(imageView); 
     return imageView; 
    } 
} 

這裏是主要的活動稱爲ThirdActivity:

import android.app.Activity; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.GridView; 

public class ThirdActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.gridview_layout); 
     final GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this)); 
     gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
       Intent i = new Intent(getApplicationContext(), FullImageActivity.class); 
       i.putExtra("id", position); 
       startActivity(i); 
      } 
     }); 
    } 
} 

這裏是FullImageActivity.java(它顯示的圖像的用戶在全屏模式下點擊) :

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.ImageView; 
import java.io.InputStream; 

public class FullImageActivity extends Activity { 

    ProgressDialog pDialog; 
    ImageView img; 
    Bitmap bitmap; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fullimageviewer); 

     Intent i = getIntent(); 
     int position = i.getExtras().getInt("id"); 
     ImageAdapter imageAdapter = new ImageAdapter(this); 

     img = (ImageView) findViewById(R.id.image); 
     String url = imageAdapter.getItem(position); 

     new DownloadImage().execute(url); 
    } 
    private class DownloadImage extends AsyncTask<String, Void, Bitmap> { 

     @Override 
     protected Bitmap doInBackground(String... URL) { 
      String imageURL = URL[0]; 
      Bitmap bitmap = null; 
      try { 
       InputStream input = new java.net.URL(imageURL).openStream(); 
       bitmap = BitmapFactory.decodeStream(input); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return bitmap; 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      img.setImageBitmap(result); 
     } 
    } 
} 

這是logcat我get.everytime我試着打開這個活動的應用程序崩潰。我在這方面很新,而且我似乎無法理解要做什麼。任何幫助是appreciated.Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.wildersdevelopment.insoletoprototype, PID: 3244 
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 
    at com.wildersdevelopment.insoletoprototype.ImageAdapter.getItem(ImageAdapter.java:31) 
    at com.wildersdevelopment.insoletoprototype.ImageAdapter.getView(ImageAdapter.java:48) 
    at android.widget.AbsListView.obtainView(AbsListView.java:2346) 
    at android.widget.GridView.makeAndAddView(GridView.java:1439) 
    at android.widget.GridView.makeRow(GridView.java:366) 
    at android.widget.GridView.fillDown(GridView.java:307) 
    at android.widget.GridView.fillFromTop(GridView.java:442) 
    at android.widget.GridView.layoutChildren(GridView.java:1282) 
    at android.widget.AbsListView.onLayout(AbsListView.java:2148) 
    at android.view.View.layout(View.java:16630) 
    at android.view.ViewGroup.layout(ViewGroup.java:5437) 
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
    at android.view.View.layout(View.java:16630) 
    at android.view.ViewGroup.layout(ViewGroup.java:5437) 
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) 
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) 
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) 
    at android.view.View.layout(View.java:16630) 
    at android.view.ViewGroup.layout(ViewGroup.java:5437) 
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
    at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) 
    at android.view.View.layout(View.java:16630) 
    at android.view.ViewGroup.layout(ViewGroup.java:5437) 
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
    at android.view.Choreographer.doFrame(Choreographer.java:606) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

回答

1

你宣佈你的數組中只有一個形象:

public static String[] mThumbIds = { 
     "http://imageshack.com/a/img923/2796/PB1lxo.jpg" 
}; 

但你提到你的整個圖像的數量是7:

int imageTotal = 7; 

getItem它試圖得到第二個元素(index = 1),它會給出異常,因爲索引不存在於你的數組中。

要麼設置imageTotal = 1要麼在字符串數組中定義7個字符串。

或者你canchange您getCount將到:

public int getCount() { 
    return mThumbIds.length; 
} 
+0

謝謝主席先生的如此有幫助。如果我可能會問,如果我想從數據庫中獲取這些圖像,該怎麼辦?所以,當我發佈我的應用程序時,圖像會自動下載,因爲我不斷添加它們? – Bembis668

+0

@ Bembis668如果你想使用一個數據庫你應該使用CursorLoader需要重新設計你的代碼 – Pooya

+0

我很抱歉,但我該怎麼做? – Bembis668