我使用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);
}
bSave是按钮。
在日志中,我有NullPointerException
行 - bSave.setEnabled(false)
:
06-30 16:20:45.414:D / STS_SessionDetails(15876):processSessionCompleted 06-30 16:20:45.414:D / AndroidRuntime(15876):关闭VM 06-30 16:20:45.414:W / dalvikvm(15876):threadid = 1:线程退出,未捕获异常(组= 0x41b62d88) 06-30 16:20:45.414:E / AndroidRuntime(15876):例外 06-30 16:20:45.414:E / AndroidRuntime(15876):致命异常:主要 06-30 16:20:45.414:E / AndroidRuntime(15876):进程: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)
看起来我丢了东西却不知道是什么。 请帮忙。
答案 0 :(得分:0)
该行
fragSessionDetails = (SessionDetails) mSectionsPagerAdapter.getItem(0);
非常糟糕的做法。更好的方法是通过更改方法来访问运行时中的Fragment:
public void setSessionCompleted(){
((SessionDetails) mSectionsPagerAdapter.getItem(0)).processSessionCompleted();
}
答案 1 :(得分:0)
最后,我有一个解决方案。
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();
}
}