使用 FragmentPagerAdapter 从父片段正确调用片段选项卡中的函数
Correctly to call function in fragment tab from parent fragment with FragmentPagerAdapter
我使用 FragmentPagerAdapter
:
创建了标签页
public class SectionsPagerAdapter extends FragmentPagerAdapter {
static final int PAGE_COUNT = 2;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new SessionDetails();
case 1:
return new SessionScoreTable();
}
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}}
我需要通过单击另一个选项卡上的按钮来更新一个选项卡。
父 FragmentActivity
有:
fragSessionDetails = (SessionDetails) mSectionsPagerAdapter.getItem(0);
并且有 function
:
public void setSessionCompleted(){
fragSessionDetails.processSessionCompleted();
}
这是从另一个选项卡 (SessionScoreTable
) 调用的。
SessionDetails
选项卡具有功能:
public void processSessionCompleted(){
Log.d(TAG,"processSessionCompleted" );
bSave.setEnabled(false);
}
b保存按钮。
在日志中我有 NullPointerException
行 - bSave.setEnabled(false)
:
06-30 16:20:45.414: D/STS_SessionDetails(15876): processSessionCompleted
06-30 16:20:45.414: D/AndroidRuntime(15876): Shutting down VM
06-30 16:20:45.414: W/dalvikvm(15876): threadid=1: thread exiting with uncaught exception (group=0x41b62d88)
06-30 16:20:45.414: E/AndroidRuntime(15876): Exception
06-30 16:20:45.414: E/AndroidRuntime(15876): FATAL EXCEPTION: main
06-30 16:20:45.414: E/AndroidRuntime(15876): Process: com.vvv.vvvvvvvvvvvvv, PID: 15876
06-30 16:20:45.414: E/AndroidRuntime(15876): java.lang.NullPointerException
06-30 16:20:45.414: E/AndroidRuntime(15876): at com.vvv.vvvvvvvvvvvvv.SessionDetails.processSessionCompleted(SessionDetails.java:237)
06-30 16:20:45.414: E/AndroidRuntime(15876): at com.vvv.vvvvvvvvvvvvv.Session.setSessionCompleted(Session.java:151)
我好像丢了什么东西,但不知道是什么东西。
请帮忙。
行
fragSessionDetails = (SessionDetails) mSectionsPagerAdapter.getItem(0);
是非常糟糕的做法。更好的方法是通过更改方法在运行时访问片段:
public void setSessionCompleted(){
((SessionDetails) mSectionsPagerAdapter.getItem(0)).processSessionCompleted();
}
终于,我找到了解决方案。
public class SectionsPagerAdapter extends FragmentPagerAdapter {
static final int PAGE_COUNT = 2;
private SessionDetails fragSessionDetails;
private SessionScoreTable fragSessionScoreTable;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new SessionDetails();
case 1:
return new SessionScoreTable();
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// save the appropriate reference depending on position
switch (position) {
case 0:
fragSessionDetails = (SessionDetails) createdFragment;
break;
case 1:
fragSessionScoreTable = (SessionScoreTable) createdFragment;
break;
}
return createdFragment;
}
public SessionDetails getFragSessionDetails(){
return fragSessionDetails;
}
public SessionScoreTable getFragSessionScoreTable(){
return fragSessionScoreTable;
}}
和
public void setSessionCompleted(){
SessionDetails fragSessionDetails = mSectionsPagerAdapter.getFragSessionDetails();
if (fragSessionDetails != null) {
Log.d(TAG, "fragSessionDetails");
fragSessionDetails.processSessionCompleted();
}
}
想法来自
我使用 FragmentPagerAdapter
:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
static final int PAGE_COUNT = 2;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new SessionDetails();
case 1:
return new SessionScoreTable();
}
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}}
我需要通过单击另一个选项卡上的按钮来更新一个选项卡。
父 FragmentActivity
有:
fragSessionDetails = (SessionDetails) mSectionsPagerAdapter.getItem(0);
并且有 function
:
public void setSessionCompleted(){
fragSessionDetails.processSessionCompleted();
}
这是从另一个选项卡 (SessionScoreTable
) 调用的。
SessionDetails
选项卡具有功能:
public void processSessionCompleted(){
Log.d(TAG,"processSessionCompleted" );
bSave.setEnabled(false);
}
b保存按钮。
在日志中我有 NullPointerException
行 - bSave.setEnabled(false)
:
06-30 16:20:45.414: D/STS_SessionDetails(15876): processSessionCompleted 06-30 16:20:45.414: D/AndroidRuntime(15876): Shutting down VM 06-30 16:20:45.414: W/dalvikvm(15876): threadid=1: thread exiting with uncaught exception (group=0x41b62d88) 06-30 16:20:45.414: E/AndroidRuntime(15876): Exception 06-30 16:20:45.414: E/AndroidRuntime(15876): FATAL EXCEPTION: main 06-30 16:20:45.414: E/AndroidRuntime(15876): Process: com.vvv.vvvvvvvvvvvvv, PID: 15876 06-30 16:20:45.414: E/AndroidRuntime(15876): java.lang.NullPointerException 06-30 16:20:45.414: E/AndroidRuntime(15876): at com.vvv.vvvvvvvvvvvvv.SessionDetails.processSessionCompleted(SessionDetails.java:237) 06-30 16:20:45.414: E/AndroidRuntime(15876): at com.vvv.vvvvvvvvvvvvv.Session.setSessionCompleted(Session.java:151)
我好像丢了什么东西,但不知道是什么东西。 请帮忙。
行
fragSessionDetails = (SessionDetails) mSectionsPagerAdapter.getItem(0);
是非常糟糕的做法。更好的方法是通过更改方法在运行时访问片段:
public void setSessionCompleted(){
((SessionDetails) mSectionsPagerAdapter.getItem(0)).processSessionCompleted();
}
终于,我找到了解决方案。
public class SectionsPagerAdapter extends FragmentPagerAdapter {
static final int PAGE_COUNT = 2;
private SessionDetails fragSessionDetails;
private SessionScoreTable fragSessionScoreTable;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new SessionDetails();
case 1:
return new SessionScoreTable();
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// save the appropriate reference depending on position
switch (position) {
case 0:
fragSessionDetails = (SessionDetails) createdFragment;
break;
case 1:
fragSessionScoreTable = (SessionScoreTable) createdFragment;
break;
}
return createdFragment;
}
public SessionDetails getFragSessionDetails(){
return fragSessionDetails;
}
public SessionScoreTable getFragSessionScoreTable(){
return fragSessionScoreTable;
}}
和
public void setSessionCompleted(){
SessionDetails fragSessionDetails = mSectionsPagerAdapter.getFragSessionDetails();
if (fragSessionDetails != null) {
Log.d(TAG, "fragSessionDetails");
fragSessionDetails.processSessionCompleted();
}
}
想法来自