Android Picasso:フラグメントにロードされていません
-
21-12-2019 - |
質問
私はピカッソAイメージでいくつかのより簡単なメモリ管理を試みています。私はちょうど私の断片の中にそれを実装しようとしてきましたが、私はそれを働かせるように思われることはできません。
mainLayout.setBackground(new BitmapDrawable(getResources(), Picasso.with(mainLayout.getContext()).load(R.drawable.background2).get()));
.
ここで、MainLayoutはlinearyayoutです。私もこれを試してみました:
Picasso.with(getActivity().getApplicationContext()).load(R.drawable.background2).into(imageView1);
.
picasso.withを試しました(これは)...しかしこれはまったくうまくいきません。
:
の例外を維持し続けますjava.lang.IllegalStateException: Method call should not happen from the main thread.
at com.squareup.picasso.Utils.checkNotMain(Utils.java:71)
at com.squareup.picasso.RequestCreator.get(RequestCreator.java:206)
at ...
.
ここで私はそれを呼んだ。
誰もが経験しているか、または破片でこれを正しく使う方法を知っていますか?
解決
例外メッセージはそれをきれいに明確にしています。 .get()メッセージは非同期ではなく、メインスレッド上の作業(ネットワーク、読み取りファイル、...)を実行します。可能な限り避けるべきです。
画像を画像ビューに設定するためのあなたのコードは私にとって正しいようです。
MainLayout(Picassoが自動的に描画可能/ビットマップをバックグラウンドに設定する)でも同じことが可能だと思います。 ここで間違っている場合は、Picassoに付属のtarget.classを見てください。イメージをターゲットにロードすることができます。これにより、成功とエラーのためにハンドラを提供する必要があります。サクセスハンドラ内では、ロードされたらビットマップを入手し、それをバックグラウンドに設定できます。
あなたの症例で働くかもしれないいくつかの解決策があります。
[編集]
Picassoによって提供されるget()メソッドを使用する場合、ロードは現在働いているスレッドで行われます。これは、ソース内で明確に記載されています。 https://github.com/square/ Picasso / BLOB /マスター/ PICASSO / SRC / MAIN / JAVA / COM / SQUARUP / PICASSO / requestCreator.java
/ **この要求を同期的に満たします。メインスレッドから呼び出さないでください。 * /
公開ビットマップget()ioException {[...]}
あなたの場合私はターゲットインタフェースを使うでしょう、そしてそれは画像がロードされたときにコールバックを提供します。
Picasso.with(getActivity()).load(R.drawable.background2).into(new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
});
.
あなたがここに内部リソースを使用しているように、なぜ次のことをしないのですか?
mainLayout.setBackgroundResource(R.drawable.background2);
.
これが役立つことを願っています。
wares
他のヒント
それを試してみてください
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.news_img);
holder.tvName = (TextView) v.findViewById(R.id.news_title);
holder.tvDate = (TextView) v.findViewById(R.id.news_time);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder = (ViewHolder )v.getTag();
Picasso.with(context).load(postList.get(position).getThumbnail()).into(holder.imageview);
holder.tvName.setText(postList.get(position).getTitle());
holder.tvDate.setText(postList.get(position).getDate());
return v;
}
.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.news_img);
holder.tvName = (TextView) v.findViewById(R.id.news_title);
holder.tvDate = (TextView) v.findViewById(R.id.news_time);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder = (ViewHolder )v.getTag();
Picasso.with(context).load(postList.get(position).getThumbnail()).into(holder.imageview);
holder.tvName.setText(postList.get(position).getTitle());
holder.tvDate.setText(postList.get(position).getDate());
return v;
}