我正在考虑一个解决方案,如何实现两个不同的活动,以便我可以像浏览器一样在它们之间滑动。
我在 Eclipse 中创建了两个项目。在其中一个中,我实现了一个简单的音乐播放器,它显示了 mp3.file 的封面。它还会在播放歌曲时显示搜索栏的移动。在另一个项目中,我实现了一个 openstreetmap 活动,它在地图上显示了我的位置。我想要的是结合这两个活动,这样我就可以在它们之间滑动。那可能吗?我必须如何解决它?提供有关使用片段的其他解决方案?我将非常感谢您提前提供建议并原谅我的英语:)
编辑:为了更好地理解:
我有一个项目,它播放一首歌曲并显示封面、歌曲名称、解释器和一个搜索栏,它在歌曲播放时移动。它仍然工作正常。我想要的是把一个布局分成两个这样的 布局。在图片中,您可以看到我实现的播放器(在左侧)。我想拆分相同的布局组件,以便在最后(右侧)有 2 个布局。我用片段尝试过。这是我当前的代码:
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);
}
}
}
当我在 PlayerFragment 中分配按钮 ID 时,我变成了 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)
这正是我的问题。我不知道在哪里分配它们或在哪里编写函数。sry 伙计们,但我真的不明白它是如何与片段一起工作的。希望你能理解并帮助我:/非常感谢