我正在尝试替换视图中的片段,但还没有成功。
我有以下课程。首先是我的PagerAdapter:
package com.example.tab1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class SectionsPagerAdapter extends FragmentPagerAdapter {
static final int NUM_ITEMS = 3;
private final FragmentManager mFragmentManager;
private Fragment mFragmentAtPos0;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
}
@Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
mFragmentAtPos0= new testFragment();
args.putInt(testFragment.ARG_SECTION_NUMBER, position + 1);
mFragmentAtPos0.setArguments(args);
return mFragmentAtPos0;
}
@Override
public int getItemPosition(Object object) {
return 0;
}
@Override
// liefert die Anzahl der Tabs
public int getCount() {
return NUM_ITEMS;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
// return getString(R.string.title_section1).toUpperCase();
// return
// Resources.getSystem().getString(R.string.title_section1).toUpperCase();
return "Suche".toUpperCase();
case 1:
// return getString(R.string.title_section2).toUpperCase();
// return
// Resources.getSystem().getString(R.string.title_section2).toUpperCase();
return "Ergebnis".toUpperCase();
case 2:
// return
// Resources.getSystem().getString(R.string.title_section3).toUpperCase();
return "Optionen".toUpperCase();
}
return null;
}
}
其次是我的DatePickerFragment:
package com.example.tab1;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class DatePickerFragment extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static DatePickerFragment newInstance() {
DatePickerFragment f = new DatePickerFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Log.d("DEBUG", "onCreateView");
return inflater.inflate(R.layout.datepickerfrag, container, false);
}
}
最后是我的MainActivity,我在方法onClickSearch
中激活了更改,这是我的第一个片段中的一个接口的方法,该方法在应用程序启动时显示。
package com.example.tab1;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener,testFragment.ButtonListenerS{
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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 action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab.
// We can also use ActionBar.Tab#select() to do this if we have a
// reference to the
// Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter.
// Also specify this Activity object, which implements the
// TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
actionBar.selectTab(actionBar.getTabAt(0));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/*
* Implementierung des "Suchbuttons" (non-Javadoc)
* Interaktion zwischen testFragment und MainActivity
*/
public void OnclickSearchS() {
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager,DatePickerFragment.newInstance());
ft.commit();
//mViewPager.setCurrentItem(1);
}
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the primary sections of the app.
*/
}
当应用程序运行时,我第一次按下按钮没有任何反应,当我再次执行此操作时,我看到一个空片段并在之后出错:
07-30 21:20:17.800: W/dalvikvm(32553): threadid=1: thread exiting with uncaught exception (group=0x40c671f8)
07-30 21:20:17.805: E/AndroidRuntime(32553): FATAL EXCEPTION: main
07-30 21:20:17.805: E/AndroidRuntime(32553): java.lang.NullPointerException
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:750)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.support.v4.view.ViewPager.populate(ViewPager.java:893)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1539)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1663)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1631)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1963)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1690)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1963)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1690)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1963)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1690)
07-30 21:20:17.805: E/AndroidRuntime(32553): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2063)
07-30 21:20:17.805: E/AndroidRuntime(32553): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1399)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
07-30 21:20:17.805: E/AndroidRuntime(32553): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2011)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.View.dispatchPointerEvent(View.java:5861)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3094)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2614)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:978)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:992)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.os.Looper.loop(Looper.java:137)
07-30 21:20:17.805: E/AndroidRuntime(32553): at android.app.ActivityThread.main(ActivityThread.java:4507)
07-30 21:20:17.805: E/AndroidRuntime(32553): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 21:20:17.805: E/AndroidRuntime(32553): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 21:20:17.805: E/AndroidRuntime(32553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-30 21:20:17.805: E/AndroidRuntime(32553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-30 21:20:17.805: E/AndroidRuntime(32553): at dalvik.system.NativeStart.main(Native Method)
我不知道,出了什么问题。
答案 0 :(得分:1)
当我缺少 getItemId(int position)方法重新实现时,我遇到了这个异常,它返回了默认和替换片段的不同(唯一)ID。有关进一步说明和示例实现,请参阅https://stackoverflow.com/a/11974777/685292。