У меня есть Fragment
в Activity
, где я показываю данные с RecyclerView
.Когда я нажимаю одну кнопку, она отправляет меня в Activity.В Adapter
я хочу попробовать Glide
, чтобы взять значок, но проблема в том, что Glide
вызывает сбой приложения.Если я вручную добавляю значок с setImageResource
, это не приводит к сбою приложения, но я хочу добавить с помощью Glide
, потому что значки зависят от url
.
Это код.
Adapter.class
public class ReadingListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
ArrayList<ReadingList> arrayList;
ReadingListDB readingListDB;
String BASE_URL = "https://www.google.com/s2/favicons?domain=";
public ReadingListAdapter(Context mContext, ArrayList<ReadingList> arrayList) {
this.mContext = mContext;
this.arrayList = arrayList;
}
@NonNull
@Override
public ReadingListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.reading_item, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int i) {
final ViewHolder viewHolder = (ViewHolder) holder;
readingListDB = new ReadingListDB(mContext);
viewHolder.readingListTitle.setText(arrayList.get(i).getPageTitle());
viewHolder.readingListUrl.setText(arrayList.get(i).getUrl());
String imageURL = BASE_URL + arrayList.get(i).getUrl();
viewHolder.imgFavIcon.setImageResource(R.drawable.default_favicon); //Here is the set manually
Glide.with(mContext)
.load(imageURL)
.into(viewHolder.imgFavIcon);
Activity.class
public class ActivityBookmarksFavorites extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "ActivityBookmark";
Button btnChangeBookmark, speedDial, readingList;
LinearLayout addBookmarkLayout;
ImageView addBookmark;
BookmarkDB bookmarkDB;
String data = null;
Button btnClose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookmarks);
speedDial = findViewById(R.id.btnFavSites);
readingList = findViewById(R.id.btnFavPages);
bookmarkDB = new BookmarkDB(this);
Intent receivedIntent = getIntent();
String test = receivedIntent.getStringExtra("SpeedDial");
SpeedDialFragment newFragment = new SpeedDialFragment();
ReadingListFragment newFragment2 = new ReadingListFragment();
if ("SpeedDial".equals(test)) {
getSupportFragmentManager().beginTransaction().add(R.id.frameLayoutWithFragment, newFragment).commit();
speedDial.setSelected(true);
speedDial.setClickable(true);
} else {
getSupportFragmentManager().beginTransaction().add(R.id.frameLayoutWithFragment, newFragment2).commit();
readingList.setSelected(true);
readingList.setClickable(true);
}
Fragment.class
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
paramView = inflater.inflate(R.layout.fragment_reading_list, container, false);
linLayoutEmptyLis = paramView.findViewById(R.id.linLayoutEmptyListReading);
mRecyclerView = paramView.findViewById(R.id.lvReading);
readingListAdapter = new ReadingListAdapter(mContext, arrayList);
readingListDB = new ReadingListDB(getActivity());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setHasFixedSize(true);
ReadingList readingList = new ReadingList();
DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(),
linearLayoutManager.getOrientation());
mRecyclerView.addItemDecoration(mDividerItemDecoration);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(mRecyclerView);
final Cursor data = readingListDB.getData();
Кнопка, которую нажимают и отправляют в действие.
MainActivity.class
bookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivityBookmarksFavorites.class);
startActivity(intent);
}
});
Ошибка с глиссады
You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed)