I am using a ViewPager to show fragments. When i swipe it multiple times it gives following error:
E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
--------- beginning of crash
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 6900
I am showing and caching image (using this) as well as i am using TextView to show text on Fragment.
I tried to get help from other links but could not get succeed.
I tried to duplicate your issue on my side, however I'm not getting the error but the images are not loaded. But the files got cached in my internal storage. By the way, in your case, it is advisable to use Picasso or Universal Image Loader as those libraries will handle loading, caching and even error. This may not be your direct solution to your problem, but just in case if you're looking for alternative, you can try this solution.
For the sake of simplicity, I am using Picasso. I have created an example project just in case if you need reference. You need to add compile 'com.squareup.picasso:picasso:2.5.2' in your gradle dependency;
1) Fragment
public class FragmentImage extends Fragment {
private TextView imageName;
private ImageView image;
public static final String IMAGE_URL = "link";
public static final String POSITION = "position";
private String url = null;
private int position = 0;
public static FragmentImage newInstance(String link, int position) {
// Required empty public constructor
FragmentImage fragmentImage = new FragmentImage();
Bundle bundle = new Bundle();
bundle.putString(IMAGE_URL, link);
bundle.putInt(POSITION, position);
fragmentImage.setArguments(bundle);
return fragmentImage;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(null != getArguments()){
url = getArguments().getString(IMAGE_URL);
position = getArguments().getInt(POSITION);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_image, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
image = (ImageView)view.findViewById(R.id.image);
imageName = (TextView)view.findViewById(R.id.imageName);
imageName.setText("Position "+position);
Picasso.with(getActivity())
.load(url)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(image);
}
}
2) FragmentAdapter
public class FragmentImagePager extends FragmentPagerAdapter {
private String[] imageUrls = {"https://www.ricoh.com/r_dc/caplio/r7/img/sample_04.jpg",
"http://i-cdn.phonearena.com/images/articles/47012-image/photo2.jpg",
"http://www.flooringvillage.co.uk/ekmps/shops/flooringvillage/images/request-a-sample--547-p.jpg",
"http://www.cameraegg.org/wp-content/uploads/2013/03/Canon-EOS-100D-Rebel-SL1-Sample-Image.jpg",
"http://imgsv.imaging.nikon.com/lineup/lens/singlefocal/wide/af-s_35mmf_14g/img/sample/sample4_l.jpg"};
public FragmentImagePager(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return FragmentImage.newInstance(imageUrls[position], position);
}
#Override
public int getCount() {
return imageUrls.length;
}
}
3) Activity
public class MainActivity extends AppCompatActivity{
private ViewPager fragmentList;
private FragmentImagePager fragmentImagePager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentList = (ViewPager)findViewById(R.id.fragmentList);
fragmentImagePager = new FragmentImagePager(getSupportFragmentManager());
fragmentList.setAdapter(fragmentImagePager);
fragmentList.setOffscreenPageLimit(fragmentImagePager.getCount());
}
}
Related
This is my dialog:
#SuppressLint("ValidFragment")
public class Popup extends DialogFragment {
private final int _layout;
#SuppressLint("ValidFragment")
public Popup(int layout) {
_layout = layout;
}
#SuppressLint({"ClickableViewAccessibility", "ResourceType"})
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Nullable
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(_layout, container, false);
return view;
}
And this is how I invoke it:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, Popup.ICustomTts, Popup.ITarget, Popup.IDialog, Popup.IControl {
private final Popup mPopupTurbine = new Popup(R.layout.fragment_turbine);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button turbineBtn = findViewById(R.id.turbine);
turbineBtn.setOnClickListener(view -> {
mPopupTurbine.show(getSupportFragmentManager(), "Speak");
});
When I click outside of that dialog, it will be closed. The problem is, I do some changes in that dialog (e.g. typed a text in TextView) and then I close that dialog. When I want to show it again, all the changes are lost. So how can I just hide the dialog so that the changes are still there when I re-display it?
I think in MainActivity I could add mPopupTurbine.getDialog().hide(); but where do I add this line of code?
You can use a ViewModel that lives throughout the life of your activity and remember your Popup fragment lives inside your activity.
Every time you make a change inside Popup save that data in a LiveData or Flow. Observe the LiveData inside Popup and update your UI.
You can post back entered data to your MainActivity:
#SuppressLint("ValidFragment")
public class Popup extends DialogFragment {
public Popup() {
// fragment constructor must be empty
}
private static final String LAYOUT_ID_KEY = "LAYOUT_ID_KEY";
private static final String PARAM1_KEY = "PARAM1_KEY";
public static Popup newInstance(int layoutId, String initialParam1) {
Popup popup = new Popup();
Bundle bundle = new Bundle();
bundle.putInt(LAYOUT_ID_KEY, layoutId);
bundle.putString(PARAM1_KEY, initialParam1);
popup.setArguments(bundle);
return popup;
}
private PopupCallback popupCallback;
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
popupCallback = (PopupCallback) context;
}
#Nullable
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
int layoutId = getArguments().getInt(LAYOUT_ID_KEY);
final View view = inflater.inflate(layoutId, container, false);
EditText editText = view.findViewById(R.id.edittext);
// set latest value of param1
editText.setText(getArguments().getString(PARAM1_KEY));
return view;
}
#Override
public void onDismiss(#NonNull DialogInterface dialog) {
EditText v = getView().findViewById(R.id.edittext);
// post back the param1
popupCallback.onDismissPopup(v.getText().toString());
super.onDismiss(dialog);
}
interface PopupCallback {
void onDismissPopup(String param1);
}
}
and receive and keep them in MainActivity:
public class MainActivity extends AppCompatActivity implements Popup.PopupCallback {
private String param1Backup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button turbineBtn = findViewById(R.id.turbine);
turbineBtn.setOnClickListener(view -> {
Popup.newInstance(R.layout.fragment_turbine, param1Backup).show(getSupportFragmentManager(), "Speak");
});
}
#Override
public void onDismissPopup(String param1) {
param1Backup = param1;
}
}
Use viewModel to save your current values and attach the lifecycle of viewModel with your activity to save the current state.
Purpose of viewModel is to survive the configuration changes in your app.
You can use Singleton Class to save those data temporarily and moment you invoke it check if you have saved any data in it and put it back in your dialog.
public class SingletonClass{
public boolean isDialogDone;
public string dialogTitle, dialogMsg;
public static SingletonClass getInstance(){
if (instance == null) {
synchronized(SingletonClass.class) {
if (instance == null) {
instance = new SingletonClass();
}
}
}
return instance;
}
}
Then while invoking your dialogBox, you can simply check
if(!SingletonClass.getInstance().isDialogDone){
editText.setText(SingletonClass.getInstance().dialogMsg);
}
Ofc you also need to add listener to your EditText so that you can save the msg user is typing.
I'm setting up an image adapter in my fragments. I recently learn the usefulness of Fragments and Therefore I am trying to switch from the traditional activities to a central activity with multiple fragments. But I cant get to display my images' infinite cycler view.
I have tried reading answers from previous questions here on Stack overflow (ImageAdapter cannot be applied to fragment class and ImageAdapter cannot be applied to a Fragment Class) but I didnt really understand anything perhaps because I am a beginner in android studio with no educational background in coding. I have also tried youtube and everywhere.
I havent found tutorials to do this, I understand that fragments is relatively new in android studio
This is the fragment I am trying to switch to
public TrendingFragment() {
// Required empty public constructor
}
private static final String TAG = "Trending";
HorizontalInfiniteCycleViewPager viewPager;
List<TrendingHolder> TrendingList = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragme
View view = inflater.inflate(R.layout.fragment_trending, container, false);
initData();
viewPager = view.findViewById(R.id.view_pager);
viewPager.setAdapter(new TrendingAdapter(getActivity()));
return view;
}
private void initData() {
//Adding the Images on board
TrendingList.add(new TrendingHolder(R.drawable.ad_test));
TrendingList.add(new TrendingHolder(R.drawable.burger_test));
TrendingList.add(new TrendingHolder(R.drawable.italian_test));
TrendingList.add(new TrendingHolder(R.drawable.pizza_test));
}
}
This is my adapter class
public class TrendingAdapter extends PagerAdapter {
Context context;
List<TrendingHolder> TrendingList;
private static final String TAG = "TrendingAdapter";
public TrendingAdapter(Context context, List<TrendingHolder> trendingList) {
this.context = context;
this.TrendingList = trendingList;
}
#Override
public int getCount() {
return TrendingList.size();
}
#Override
public boolean isViewFromObject(View view, #NonNull Object o){
return view.equals(o);
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
//Inflate View
View view = LayoutInflater.from(context).inflate(R.layout.card_item, container, false);
//View
ImageView trending_image = (ImageView)view.findViewById(R.id.trending_holder);
//Set Data
trending_image.setImageResource(TrendingList.get(position).getImage());
//set On Event Click
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//shop activity comes here
Log.d(TAG, "onClick: added to Cart");
}
});
container.addView(view);
return view;
}
}
This is my model for the images
public class TrendingHolder {
private int image;
public TrendingHolder(){
}
public TrendingHolder(int image) {
this.image = image;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
I am getting this error TrendingAdapter( )
in TrendingAdapter cannot be applied
to
(androidx.fragment.app.FragmentActivity) in my Trending Fragment
It seems like missing constructor parameter. The constructor of TrendingAdapter accepts 2 parameters public TrendingAdapter(Context context, List<TrendingHolder> trendingList). But you pass 1 parameter viewPager.setAdapter(new TrendingAdapter(getActivity()));.
I'm using this repo to create sliding tabs in an Android app:
https://github.com/ogaclejapan/SmartTabLayout
I used the demo code and reduced it to my needs and now I want to have only one Fragment class that handles all activities that should use sliding tabs. So I have the activity Cards and Walkthrough in my particular app I want to use sliding tabs.
The code of Cards is this:
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
public class Cards extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
Demo demo = Demo.valueOf(String.valueOf(Demo.CARDS));
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(demo.titleResId);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ViewGroup tab = (ViewGroup) findViewById(R.id.tab);
tab.addView(LayoutInflater.from(this).inflate(demo.layoutResId, tab, false));
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
demo.setup(viewPagerTab);
FragmentPagerItems pages = new FragmentPagerItems(this);
for (int titleResId : demo.tabs()) {
pages.add(FragmentPagerItem.of(getString(titleResId), DemoFragment.class));
}
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getSupportFragmentManager(), pages);
viewPager.setAdapter(adapter);
viewPagerTab.setViewPager(viewPager);
}
public enum Demo {
CARDS(R.string.window_cards, R.layout.activity_slide);
public final int titleResId;
public final int layoutResId;
Demo(int titleResId, int layoutResId) {
this.titleResId = titleResId;
this.layoutResId = layoutResId;
}
public static int[] tabTitle() {
return new int[] {
R.string.title_cards1,
R.string.title_cards2,
R.string.title_cards3,
R.string.title_cards4,
R.string.title_cards5,
R.string.title_cards6
};
}
public void setup(final SmartTabLayout layout) { }
public int[] tabs() { return tabTitle(); }
}
}
And the other class is called DemoFragment:
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
public class DemoFragment extends Fragment {
private static final String TAG = "Cards";
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_demo, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
int position = FragmentPagerItem.getPosition(getArguments());
WebView browser = (WebView) view.findViewById(R.id.webView);
browser.loadUrl("file:///android_asset/"+title+position+".html");
}
}
Now I just want to use this DemoFragment class this way that it will load the webview depending on the activity I'm in right now. But I don't know how to get the variable, like Cards in this example, just the current position of the tab with int position = FragmentPagerItem.getPosition(getArguments());.
I have not used this library, but it looks like you can supply your own arguments Bundle to the Fragments using on overloaded version of FragmentPagerItem.of().
for (int titleResId : demo.tabs()) {
Bundle args = new Bundle();
args.putString("page_source", ...);
pages.add(FragmentPagerItem.of(getString(titleResId), DemoFragment.class, args));
}
WebView browser = (WebView) view.findViewById(R.id.webView);
browser.loadUrl(getArguments().getString("page_source"));
for the sake of not duplicating the code I've decided in my program to create a BaseFragment (which extends Fragment) to hold for some boilerplate data and provide proper handling of pause/resume, as well as the newInstance(String myPram) method. Long story short: The role of the BaseFragment is to ensure the saving and restoring of the state, i.e. restoring myPramas a local variable...
What I wanted to do next was to extend that BaseFragment in order to handle two different types of layout for two different Fragments (each of the needs the handling of the MyParam filed): EditFragment and DetailFragment.
The problem arises now, because no View is inflated for the Fragment, though they are added as a transaction (so they are transparent!).
EditFragment and DetailFragment both provide the onCreateView() method, in order to build their own views, I am they are correct since I managed to create them without the "subclassing mechanism", and of course BaseFragment does not provide an implementation of the onCreateView() but no result, they draw no UI.
I tried putting some super.onCreateView() here and there, with no result as well...What am I doing wrong?
Here's the base Fragment:
public class TimetableBaseFragment extends Fragment {
public static String ID_FIELD = "id";
public static String ID_DESC = "desc";
private String id;
private String desc;
// Constructor to deliver previous position along with itself
public static Fragment newInstance(String id, String desc) {
TimetableBaseFragment f = new TimetableBaseFragment();
Bundle args = new Bundle();
args.putString(ID_FIELD, id);
args.putString(ID_DESC, desc);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
id = args.getString(ID_FIELD);
desc = args.getString(ID_DESC);
}
if (savedInstanceState != null) {
id = savedInstanceState.getString(ID_FIELD);
desc = savedInstanceState.getString(ID_DESC);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ID_FIELD, id);
outState.putString(ID_DESC, desc);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
id = savedInstanceState.getString(ID_FIELD);
desc = savedInstanceState.getString(ID_DESC);
}
}
public String getExamId() {
return id;
}
public String getExamDesc() {
return desc;
}
}
And the one I am trying to add is:
public class TimetableEditFragment extends TimetableBaseFragment {
private LinearLayout mLlEditEmpty;
private ArrayList<ScheduleUser> lessons;
private TimetableEditRecyclerViewAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lessons = new ArrayList<>();
mAdapter = new TimetableEditRecyclerViewAdapter(getActivity());
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_timetable_edit, container, false);
mLlEditEmpty = (LinearLayout) rootView.findViewById(R.id.LOGIC_empty);
RecyclerView mRvEdit = (RecyclerView) rootView.findViewById(R.id.HEADER_rv);
mRvEdit.setHasFixedSize(true);
mRvEdit.setLayoutManager(new LinearLayoutManager(getActivity()));
mRvEdit.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
mRvEdit.setAdapter(mAdapter);
Toolbar examDesc = (Toolbar) rootView.findViewById(R.id.HEADER_examDesc);
examDesc.setTitle("TOOLBAR Testing");
FloatingActionButton add_fab = (FloatingActionButton) rootView.findViewById(R.id.EDIT_fab);
add_fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addLesson(new ScheduleUser());
}
});
return rootView;
}
public void addLesson(ScheduleUser newItem) {
lessons.add(newItem);
mAdapter.notifyItemInserted(lessons.size() - 1);
}
public class TimetableEditRecyclerViewAdapter extends RecyclerView.Adapter<TimetableEditRecyclerViewAdapter.ViewHolder> {
/* Lots of stuff */
}
}
PS: I've noticed that it's very likely that the child onCreateView() does not get called at all, since I had an error, which was due to be fund at runtime, but the system did not notify.
Please help!
I have been trying for 2 hours now to find the null pointer in my code and am having no luck. I am hoping one of you far smarter individuals than I can figure it out. First off, I am following the tutorial here: http://youtu.be/Y2liEQV3tbQ?list=PLonJJ3BVjZW4lMlpHgL7UNQSGMERcDzHo by slideNerd.
Here is what is baffling me: When in horizontal mode, the code executes perfectly and the displays are updated with correct information. This means that the horizontal portion of the if statement is working. When it is in portrait however and I click a list view icon, it immediately force closes with null pointer. The error comes from the line that reads:
f2b.changeTextView(index);
If I remove that line, the second activity opens without a problem. I am simply at a loss as to what has been causing me so much trouble.
Here are my classes:
MainActivity:
public class MainActivity extends Activity implements FragmentA.Communicator{
private FragmentA f1a;
private FragmentB f2b;
private FragmentManager manager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=getFragmentManager();
f1a = (FragmentA) manager.findFragmentById(R.id.fragment1a);
f1a.setCommunicator(this); //the respond method that is implemented from Fragment a
f2b = (FragmentB) manager.findFragmentById(R.id.fragment2b);
}
#Override
public void respond(int index) {
//Check if portrait mode
if (f2b != null && f2b.isVisible()){ //Horizontal
f2b.changeTextView(index);
} else { //Vertical
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
Fragment A:
public class FragmentA extends Fragment implements AdapterView.OnItemClickListener {
ListView listView;
Communicator comm;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ListView) getActivity().findViewById(R.id.fragment_a_list_view);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.chapters, android.R.layout.simple_list_item_1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
comm.respond(position);
}
public void setCommunicator(Communicator communicator){
this.comm = communicator;
}
//Interface to allow access between fragments
public interface Communicator{
public void respond(int index);
}
}
Fragment B:
public class FragmentB extends Fragment {
TextView textView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textView = (TextView) getActivity().findViewById(R.id.fragment_b_text_view);
}
public void changeTextView(int position){
String [] descriptions = getResources().getStringArray(R.array.descriptions);
textView.setText(descriptions[position]);
}
}
Activity2:
public class Activity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Intent intent = getIntent();
int index = intent.getIntExtra("index", 0);
//reference the fragment
FragmentB f2b = (FragmentB) getFragmentManager().findFragmentById(R.id.fragment2b);
if (f2b != null){
f2b.changeTextView(index);
}
}
}
And Here is the error code in the logcat:
Caused by: java.lang.NullPointerException
at com.pgmacdesign.fragmentexamples3_flexibleui.FragmentB.changeTextView(FragmentB.java:42)
at com.pgmacdesign.fragmentexamples3_flexibleui.Activity2.onCreate(Activity2.java:25)
I have already confirmed that the index/ position variable is passing correctly. If anyone has any suggestions as to how to find the null pointer error, I would love the assistance.
In your Fragment B,
Try to replace textView = (TextView) getActivity().findViewById(R.id.fragment_b_text_view) by textView = (TextView) view.findViewById(R.id.fragment_b_text_view) and move it from onActivityCreated() to onCreateView().
Hope this helps.
I think your problem is when you can try to find the ID.
Try this textView = (TextView) view.findViewById(R.id.fragment_b_texT_view) where this view is your View view = inflater.inflate(R.layout.fragment_b, container, false);.