2016-09-28 19 views
3

画像を見るたびにアプリケーションが終了します。なぜ説明できますか?Glide on Androidアプリでクラッシュする

おそらく問題は文字列Glide.with(getApplicationContext())であると思われますが、それは正しくない可能性があります。 Android Studioの場合、エラーはありません。

他のすべての要素は正常ですが、問題は滑っているだけです。

public class MainActivity extends AppCompatActivity { 

    private final String url= "MY ARRAY"; 
    ... 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ... 
     new gson().execute(url); 
    } 

    public class gson extends AsyncTask<String,String, List<MovieModel> >{ 

    .... 
public class Adapter extends ArrayAdapter{ 
    private List<MovieModel> movieModelList; 
    private int resource; 
    private LayoutInflater inflater; 
    public Adapter(Context context, int resource, List<MovieModel> objects) { 
     super(context, resource, objects); 
     movieModelList = objects; 
     this.resource = resource; 
     inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     if(convertView == null){ 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(resource, null); 
      holder.fruitsfoto = (ImageView)convertView.findViewById(R.id.fruits); 
      holder.fruitsname = (TextView)convertView.findViewById(R.id.name); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     ImageView imageView = (ImageView) findViewById(R.id.pics); 
     final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar); 
try { 
Glide.with(MainActivity.this)//getApplicationContext() 
     .load(ModelList.get(position).getImage()) 
     .listener(new RequestListener<String, GlideDrawable>() { 
    @Override 
    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) { 
     return false; 
    } 
    @Override 
    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { 
     progressBar.setVisibility(View.GONE); 
     return false; 
    } 
}) 
     .into(imageView); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
holder.fruitsname.setText(ModelList.get(position).getName()); 
return convertView; 
      } 
     } 
    } 

更新:表示画像は配列のリストの後にありません。解決策が必要です。

public Adapter(Context context, int resource, List<MovieModel> objects) { 
     super(context, resource, objects); 
     ModelList = objects; 
     this.resource = resource; 
     inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    } 
..... 
    try { 
    Glide.with(MainActivity.this)//getApplicationContext() 
      .load(ModelList.get(position).getFruits()) 
      .into(imageView); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

編集:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
    if(convertView == null){ 
     holder = new ViewHolder(); 
     convertView = inflater.inflate(resource, null); 
     holder.fruitsfoto = (ImageView)convertView.findViewById(R.id.fruits); 
     holder.fruitsname = (TextView)convertView.findViewById(R.id.name); 
1)holder.image = (TextView)convertView.findViewById(R.id.pics); 
2)ImageView imageView = (ImageView)convertView.findViewById(R.id.pics);    
convertView.setTag(holder); 
//the first cause a crash 
//the second return "Variable 'imageView is never used'" 

これは、コンソールのエラーです:

09-28 17:19:42.079 30111-30111/app E/AndroidRuntime: FATAL EXCEPTION: main 
              Process: app, PID: 30111 
              java.lang.NullPointerException 
              at app$1.onResourceReady(MainActivity.java:309) 
              at app$1.onResourceReady(MainActivity.java:301) 
              at com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:522) 
              at com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:507) 
              at com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java:158) 
              at com.bumptech.glide.load.engine.EngineJob.access$100(EngineJob.java:22) 
              at com.bumptech.glide.load.engine.EngineJob$MainThreadCallback.handleMessage(EngineJob.java:202) 
              at android.os.Handler.dispatchMessage(Handler.java:98) 
              at android.os.Looper.loop(Looper.java:146) 
              at android.app.ActivityThread.main(ActivityThread.java:5511) 
              at java.lang.reflect.Method.invokeNative(Native Method) 
              at java.lang.reflect.Method.invoke(Method.java:515) 
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
              at dalvik.system.NativeStart.main(Native Method) 
+2

ポストlogcatエラー – Nikhil

+1

Glide.with(getApplicationContext())あなたがイメージが誤って配置さに関する問題に直面している場合は、あなたのコードを共有すること自由に感じなさいGlide.with(MainActivity.this) –

+0

と交換アダプタはこちら。 –

答えて

2

、あなたのアプリケーションマニフェストにINTERNET許可を追加しているかどうかを確認してください?

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="viewpager.nested.example.com.testapplicationforso"> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     ... 
    </application> 

</manifest> 

そして、はい、それは常に良いtry/catch文の中、あなたの滑空画像読み込みコードセグメントを持っています。

try { 
    Glide.with(getApplicationContext()).load(yourUrl).into(img); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

更新:誤って配置画像について

- 私はあなたのgetView機能では、あなたが適切ImageViewを見つけられませんでしたことを見ることができました。 ImageViewconvertViewのように、このようなリストの他の項目と同様に見つける必要があります。

holder.image = (TextView)convertView.findViewById(R.id.pics); 

// Then load the image in holder.image 
Glide.with(MainActivity.this)//getApplicationContext() 
    .load(ModelList.get(position).getFruits()) 
    .into(holder.image); 
+0

ソリューションを試すためのコードを変更します。 try {Glide.with(MainActivity.this)// getApplicationContext() .load(movieModelList.get(position).getImage()) .into(imageView); } catch(例外e){ e.printStackTrace(); } 画像は表示されていますが、私は別の問題があります。 表示画像は配列のリストに従いません。たとえば、リンゴの写真がバナナリストに表示され、スワイプダウンするとバナナのリストが表示され、代わりにバナナの写真が表示されます。 正しい順序で表示されないのはなぜですか? – Question

+0

あなたの質問にもコメントを追加しました。質問の中であなたのアダプタコードを共有してください。アダプターコードで質問を編集してみてください。 –

+0

私の投稿を変更しました。 – Question

関連する問題