Can anyone tell me why this isnt working? I havent changed this file since it worked last but now I get the error below:
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
private final static int NUM_PAGES = 2;
private ViewPager mPager;
private ScreenSlidePagerAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(2);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment frag = null;
switch (i) {
case 0:
frag = new PageOneFragment();
break;
case 1:
frag = new PageTwoFragment();
break;
}
return frag;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
The error is on "frag = new PageTwoFragment();" which states "Type mismatch: cannot convert from PageTwoFragment to Fragment".
Maybe I should create two projects from now on, last good version and then current working project. Is this something other people do?
Thanks
The Problem is, that you are mixing Android Fragments how there were introduced in API11 and Fragments from android support library.
You have to use one or the other, but not both.
Change your imports to
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
And change getFragmentManager() to getSupportFragmentManager().
And it will work using the support lib.
Calling mPager.setCurrentItem(2); with a pager of 2 Fragments crashs, though.
Only 0 and 1 are valid values in your case.
You imported the wrong Fragment.
You need to import android.support.v4.Fragment
Furthermore, your adapter only handles 2 Fragments. Therefore calling
ViewPager.setCurrentItem(2);
will cause problems, since the index for the first fragment is 0.
Related
I am developing an Android app that has a little bit confusing navigation structure which leads me to my problem. There are two ways to navigate through the app. First is the BottomNav and second is a TabMenu. I thought about working with fragments that replace each other, so I came up with the following structure:
1. BottomNav#1
- TabMenu#1
- Tabmenu#2
- TabMenu#3
- ...
2. BottomNav#2
- TabMenu#4
- Tabmenu#5
- TabMenu#6
- ...
... and so on.
The problem I am facing is that when I navigate from BottomNav#1 to BottomNav#2 and back again there is a blank screen that doesn't show the content of the actual fragment:
When I open the app
After clicking on BottomNav#2 and then back to BottomNav#1 the fragment seems to be blank
My guess is that I somehow have a problem with my fragmentTransaction.replace(); as it seems like the fragment doesn't get loaded again? I am kind of new to this and really tried to find an answer online but this is a bit specific why I guess I didn't find anything.
This is my MainActivity:
package com.example.XXXX;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private FrameLayout mMainFrame;
private AusweisFragment ausweisFragment;
private SpendenFragment spendenFragment;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ausweisFragment = new AusweisFragment();
spendenFragment = new SpendenFragment();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item)
{
switch (item.getItemId())
{
case R.id.navigation_ausweis:
setFragment(ausweisFragment);
return true;
case R.id.navigation_spenden:
setFragment(spendenFragment);
return true;
}
return false;
}
};
private void setFragment(Fragment fragment)
{
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
This should lead to e.g. the fragment called "ausweis" by clicking on on BottomNav#1 (switch case):
package com.example.XXXXXXX;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabItem;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AusweisFragment extends Fragment {
private View rootView;
private AusweisPageAdapter ausweisPageAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;
public AusweisFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
rootView= inflater.inflate(R.layout.fragment_ausweis, container, false);
tabLayout = rootView.findViewById(R.id.tablayoutAusweis);
viewPager = rootView.findViewById(R.id.viewPagerAusweis);
ausweisPageAdapter = new AusweisPageAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(ausweisPageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return rootView;
}
}
After that I would like to call another fragment called "spenden" which is exactly the same as the fragment called "ausweis" but with different named tabs, so I think it is not bother you with more code.
edit: I missed about writing where the "content" I wrote about gets from. For a first try and proof that the fragments change, I hardcoded a phrase like "Ausweis" into the XML which is connected to my fragment java class.
Maybe one of you has an idea to that problem. I think it has something to do with my onCreateView in one of the fragments, but I have no close clue.
Hopefuly I didn't miss an important detail. I am very grateful for any kind of help. Thanks a lot in advance.
The answer to my problem was to use a child-parent relation between the different fragments. With the click on BottomNav#1 I am inflating an fragment which inflates a new fragment inside itself. That was the key problem. In my code I handled this as a second "normal" getFragmentManager();.
The answer is to use getChildFragmentManager(); for the nested fragment instead. Like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
rootView= inflater.inflate(R.layout.fragment_ausweis, container, false);
tabLayout1 = rootView.findViewById(R.id.tablayoutAusweis);
viewPager = rootView.findViewById(R.id.viewPagerAusweis);
ausweisPageAdapter = new AusweisPageAdapter(getChildFragmentManager(), tabLayout1.getTabCount());
viewPager.setAdapter(ausweisPageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout1));
...
...
Hopefuly this will help some people when they face the same problem with nested fragments.
I am very new to Android programming and I was wondering how I am able to fix this error that I'm getting. I have scowwered the internet searching for solutions on how to compensate for the fact that I have api 15 instead of the required 17 for my FragmentTransaction. I tried importing the support.v4.app.FragmentTransaction but still no luck here is the code:
package com.hfad.workout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.support.v4.app.FragmentTransaction;
import android.app.Fragment;
public class WorkoutDetailFragment extends Fragment {
private long workoutId;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if(savedInstanceState != null)
{
workoutId = savedInstanceState.getLong("workoutId");
}
android.support.v4.app.FragmentTransaction ft = getChildFragmentManager().beginTransaction();
StopwatchFragment stopwatchFragment = new StopwatchFragment();
ft.replace(R.id.stopwatch_container, stopwatchFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
return inflater.inflate(R.layout.fragment_workout_detail, container, false);
}
}
Any reason as to why the support.v4.app might not be working? Is there a work around to this? Any help is greatly appreciated :D
You have use "import android.app.Fragment" for your fragment.That's why your support.v4.appnot working here.
Try to import android.support.v4.app.Fragment for using android.support.v4.app.FragmentTransaction.
You can try to chaging from
import android.app.Fragment;
public class WorkoutDetailFragment extends Fragment {
to
import android.support.v4.app;
public class WorkoutDetailFragment extends Fragment {
I am getting a cannot resolve symbol error on CrimeFragment and I don't know why. I am a beginner so I'm not sure how to fix this. Here is the code for CrimeActivity.java
package com.bignerdranch.android.criminalintent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class CrimeActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = new CrimeFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
}
Seems like you aren't importing your class CrimeFragment
Make sure that CrimeFragment.java and Crime.java are in the right packages. Select "Packages" view in the top-left corner. If these files hang around separately at the bottom, drag and drop them into the "com.(..).criminalintent" package.
I have been trying to build a Master-Detail f low in android but wanted to change one of the detail fragments to a different fragment. As this is one of my first Android applications, I was just trying to make a picture appear on this new fragment. For this, I built the following two classes
1) Fragment class ( displays the picture to be displayed )
package com.userName.miscapp;
import com.userName.miscapp.dummy.DummyContent;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PictureFragment extends Fragment {
// Default Copy Constructor for the fragment
public PictureFragment() {
}
#Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState )
{
View rootView = inflater.inflate(R.layout.fragment_simple_picture, container, false);
return rootView;
}
}
2) Activity to display the same
package com.userName.miscapp;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class SimplePicture extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
PictureFragment frag = new PictureFragment();
frag.setArguments(arguments);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.simple_picture_container,frag).commit();
//setContentView(R.layout.activity_simple_picture);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.simple_picture, menu);
return true;
}
}
On compilation, it does not recognize the PictureFragment to be an extension of Fragment class. This is inspite of it being clearly written in the first file. Searching for solutions on Stackoverflow said to extend FragmentActivity instead of Activity and trying to use getSupportFragmentManager() neither of which helped.
PS : Using 11 as the base for the current application.
Any help would be appreciated
Thanks
This is because you are using android.app.Fragment from new API in conjunction with android.support.v4.app.FragmentManager from support library, you should replace import in your PictureFragment from android.app.Fragment to android.support.v4.app.Fragment to make it work.
I am making a view pager to make a slide show for images. I took code from Android developers, but I was facing some issues, fragment was not recognized, I think it was because my android was 2.33. So to solve that I imported a jar file android.support.v4.jar
My issues were resolved but now I am getting this error that getfragmentmanager() is undefined
and another issue "The method invalidateOptionsMenu() is undefined for the type new ViewPager.SimpleOnPageChangeListener(){}"
Here is my code, can any one please help ??
My platform is 2.3.3 and api level is 10 and in manifest I have this
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
Code :
package com.example.profilemanagment;
import android.support.v4.app.Fragment;
import android.support.v4.app.*;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
public class ScreenSlideActivity extends FragmentActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 5;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When changing pages, reset the action bar actions since they are dependent
// on which page is currently active. An alternative approach is to have each
// fragment expose actions itself (rather than the activity exposing actions),
// but for simplicity, the activity provides the actions in this sample.
invalidateOptionsMenu();
}
});
}
/**
* A simple pager adapter that represents 5 {#link ScreenSlidePageFragment} objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Quoting from the docs.
When using this class (FragmentActivity) as opposed to new platform's built-in fragment and loader support, you must use the getSupportFragmentManager() and getSupportLoaderManager() methods respectively to access those features.
Since you are extending FragmentActivity use getSupportFragmentManager()
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
Check the docs
FragmentActivity does not have getFragmentManager()