I have a Fragment
into an Activity
which there I show data from RecyclerView
. When I click in one button it will send me to the Activity. In the Adapter
I want to try the Glide
to take an icon but the problem is that the Glide
it is crashing the app. If I manually add a icon with setImageResource
it is not crashing the app but I want to add with Glide
because the icons depends from the url
.
This is the code.
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();
Button which is clicked and to send to the Activity.
MainActivity.class
bookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivityBookmarksFavorites.class);
startActivity(intent);
}
});
Error from the Glide
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)
Your are using an uninitialised variable (mContext) in onCreateView's 4th line of your Fragment.class. Just initialise it before or just call getActivity() to provide a valid Context.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.