i am thinking about a solution, how to implement two different activities, so that i can swipe between them like a viewpager.
i created two projects in eclipse. in one of them, i implemented a simple musicplayer, which shows the cover of the mp3.file. it displays also the move of a seekbar when its playing a song.
in the other project i implemented an openstreetmap activity, which shows me my location on a map. what i want is to combine this two activities, so that i can swipe between them. is that possible? how i have to approach to solve it? Gives other solutions as to using fragments? I would be very grateful for advices
thanks in advance and forgive me for my english 🙂
EDIT: For a better comprehension:
i have an project, which plays a song and displays the cover, songname, interpreter and a seekbar, which moves while the song is playing. it is still working fine. what i want is to split the one layout in two layouts like
this . in the picture you can see the player what i implemented (on the left side). i want to split same layout-components, so that i have 2 layouts at the end (right side).i tried it with fragments. here is my current code:
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.swipeview3.R.layout.main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(com.example.swipeview3.R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(com.example.swipeview3.R.menu.main, menu);
return true;
}
// ------------------------------#########################------------------------------
// ------------------------------#########################------------------------------
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 0) {
PlayerFragment f = new PlayerFragment(position);
return f;
}
if(position == 1) {
SongParameterFragment f = new SongParameterFragment(position);
return f;
}
else {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(com.example.swipeview3.R.string.title_section1).toUpperCase();
case 1:
return getString(com.example.swipeview3.R.string.title_section2).toUpperCase();
case 2:
return getString(com.example.swipeview3.R.string.title_section3).toUpperCase();
}
return null;
}
}
// ------------------------------#########################------------------------------
// ------------------------------#########################------------------------------
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return textView;
}
}
// ------------------------------#########################------------------------------
// ------------------------------#########################------------------------------
public static class PlayerFragment extends Fragment {
int fragmentNR;
Button btnPlay, btnStop, btnExit;
SeekBar seekBar;
public PlayerFragment(int position) {
this.fragmentNR = position;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
btnPlay = (Button)getView().findViewById(com.example.swipeview3.R.id.button_play);
btnStop = (Button)getView().findViewById(com.example.swipeview3.R.id.button_stop);
btnExit = (Button)getView().findViewById(com.example.swipeview3.R.id.button_exit);
return inflater.inflate(com.example.swipeview3.R.layout.player, container, false);
}
}
// ------------------------------#########################------------------------------
// ------------------------------#########################------------------------------
public static class SongParameterFragment extends Fragment {
int fragmentNR;
ImageView cover;
TextView songParameter, songDuration;
public SongParameterFragment(int position) {
this.fragmentNR = position;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(com.example.swipeview3.R.layout.song_parameters, container, false);
}
}
}
when i assign the button ID’s in the PlayerFragment, i became a nullpointerException
01-23 13:33:09.553: D/AndroidRuntime(15881): Shutting down VM
01-23 13:33:09.553: W/dalvikvm(15881): threadid=1: thread exiting with uncaught exception (group=0x40c571f8)
01-23 13:33:09.553: E/AndroidRuntime(15881): FATAL EXCEPTION: main
01-23 13:33:09.553: E/AndroidRuntime(15881): java.lang.NullPointerException
01-23 13:33:09.553: E/AndroidRuntime(15881): at com.example.swipeview3.MainActivity$PlayerFragment.onCreateView(MainActivity.java:142)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 13:33:09.553: E/AndroidRuntime(15881): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2257)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.View.measure(View.java:12929)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.os.Looper.loop(Looper.java:137)
01-23 13:33:09.553: E/AndroidRuntime(15881): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-23 13:33:09.553: E/AndroidRuntime(15881): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 13:33:09.553: E/AndroidRuntime(15881): at java.lang.reflect.Method.invoke(Method.java:511)
01-23 13:33:09.553: E/AndroidRuntime(15881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-23 13:33:09.553: E/AndroidRuntime(15881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-23 13:33:09.553: E/AndroidRuntime(15881): at dalvik.system.NativeStart.main(Native Method)
that is exactly my problem. I do not know where to assign them or where the functions are to be written. sry guys, but i really dont understand how that works with fragments. hopefully you have understanding and can help me :/ thanks a lot
Look at this example
Now make a new FirstActivity.java