こんにちは、FragmentActivity から int を取得しようとしています。これを行う方法はありますが、コードが非常に雑然として長くなり、問題を短縮するためにこれを行いました。エディターではエラーは発生しませんが、実行するとエラーが発生しますアプリが著しくクラッシュします。助言がありますか?
動かないコード例
MainActivity mainActivity = ((MainActivity)getActivity());
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.upgrades_fragment, container, false);
TextView AirFreshenersCost = (TextView) view.findViewById(R.id.AirFreshenersCost );
if(mainActivity.amountAirFresheners == 5){
AirFreshenersCost.setText("5");
}
return view;
}
LogCat
10-27 22:16:37.333: E/AndroidRuntime(5593): FATAL EXCEPTION: main
10-27 22:16:37.333: E/AndroidRuntime(5593): java.lang.NullPointerException
10-27 22:16:37.333: E/AndroidRuntime(5593): at com.free.dennisg.clickingbad.fragments.UpgradesFragment.onCreateView(UpgradesFragment.java:40)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.view.Choreographer.doFrame(Choreographer.java:531)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.os.Handler.handleCallback(Handler.java:730)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.os.Looper.loop(Looper.java:137)
10-27 22:16:37.333: E/AndroidRuntime(5593): at android.app.ActivityThread.main(ActivityThread.java:5289)
10-27 22:16:37.333: E/AndroidRuntime(5593): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 22:16:37.333: E/AndroidRuntime(5593): at java.lang.reflect.Method.invoke(Method.java:525)
10-27 22:16:37.333: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-27 22:16:37.333: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-27 22:16:37.333: E/AndroidRuntime(5593): at dalvik.system.NativeStart.main(Native Method)
動作するコードの例
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.upgrades_fragment, container, false);
TextView AirFreshenersCost = (TextView) view.findViewById(R.id.AirFreshenersCost );
if(((MainActivity)getActivity()).amountAirFresheners == 5){
AirFreshenersCost.setText("5");
}
return view;
}