如果在OnCreateView
方法(或OnActivtivityCreated
方法之外)调用任何类型的视图,我已经尝试了两种方法,我得到NullPointerException
。为了在片段之间进行通信,我使用了接口方法,通过我的研究,它似乎是最好的方法。不幸的是,我怀疑我的错误来自于MainActivity
找到碎片的方式。
为了尝试调试我到目前为止的内容,我在System.out.println()
中的对象上使用OnCreateView()/OnActivityCreated()
并且有一个内存地址意味着它存在并且不是null,但是当我在外面调用它时来自MainActivity
的函数,它变为空......?
有人可以解释我的情况吗?谢谢! (顺便说一句,我已经把代码的一些无用的部分拿出来了)
Tab2Home.java
@Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
spinner = (Spinner)getActivity().findViewById(R.id.tipSpinner);
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.tipArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
serviceRate=(RatingBar) getActivity().findViewById(R.id.serviceBar);
//THE OBJECT BELOW IS WHERE THE NULLPOINTEREXCEPTION IS COMING FROM
billAmount = (EditText)getActivity().findViewById(R.id.billEditText);
System.out.println(billAmount); //Surprisingly, this is not null IN the method.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
}
public void transferInfo(){
double bill;
if (String.valueOf(billAmount.getText()).isEmpty()) //billAmount is null OUTSIDE of the method.
bill = 0.00;
else
bill = Double.parseDouble(String.valueOf(billAmount.getText()));
odsl.setData( //odsl is the OnDataSetListener object.
bill,
Integer.parseInt(String.valueOf(spinner.getItemAtPosition(3))),
Integer.parseInt(String.valueOf(numPeopleEdit.getText()))
);
}
public interface OnDataSetListener {
public void setData(double bill, int tip, int ppl);
}
@Override
public void onAttach(Context context){
super.onAttach(context);
try {
odsl = (OnDataSetListener) context;
} catch (Exception e){}
}
和MainActivity.java
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(1);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == 2){
Tab2Home homefrag = (Tab2Home) mSectionsPagerAdapter.getItem(1);
homefrag.transferInfo();
System.out.println(".transferInfo() has been called");
}
}
@Override
public void onPageSelected(int position) {
if (position == 3){
Tab2Home homefrag = (Tab2Home) mSectionsPagerAdapter.getItem(2);
homefrag.transferInfo();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void setData(double bill, int tip, int ppl) {
Tab3Summary summaryfrag = (Tab3Summary) mSectionsPagerAdapter.getItem(2);
summaryfrag.updateInfo(bill, tip, ppl);
}
编辑:
以下是错误消息:
05-23 02:28:00.352 31339-31339/com.uottawa.bond.simpletip E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.uottawa.bond.simpletip, PID: 31339
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.uottawa.bond.simpletip.Tab2Home.transferInfo(Tab2Home.java:97)
at com.uottawa.bond.simpletip.MainActivity$1.onPageScrolled(MainActivity.java:64)
at android.support.v4.view.ViewPager.dispatchOnPageScrolled(ViewPager.java:1954)
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1928)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1866)
at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1826)
at android.view.View.updateDisplayListIfDirty(View.java:16037)
at android.view.View.draw(View.java:16834)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1195)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.draw(View.java:17071)
at android.view.View.updateDisplayListIfDirty(View.java:16050)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728)
at android.view.View.updateDisplayListIfDirty(View.java:16013)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2796)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:0)
找到解决方案:
1.重复以下方法
2.取代getActivity()。findviewbYId使用 rootView
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
spinner = (Spinner)rootView.findViewById(R.id.tipSpinner);
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.tipArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
serviceRate=(RatingBar) rootView.findViewById(R.id.serviceBar);
billAmount = (EditText)rootView.findViewById(R.id.billEditText);
System.out.println(billAmount);
}