从片段中的FragmentActivity获取int的更好方法是什么?

时间:2013-10-27 21:20:22

标签: android android-fragments int android-fragmentactivity

您好我试图从我的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;
}

2 个答案:

答案 0 :(得分:0)

您可以将int存储为MainActivity上的静态变量。然后,您可以引用它而不是引用公共实例变量。

if (MainActivity.AIR_FRESHENER_COUNT == 5) { ...

我认为崩溃的原因是你持有对活动的引用,即使你的片段存在,活动也可以被创建和销毁,因此引用可能为空(很难说明究竟是什么) NPE在不知道原始文件中的行号的情况下开启。

答案 1 :(得分:0)

我认为你应该定义一个接口来封装你的活动和片段。由于活动的概念不应该知道她的碎片的细节,也不应该知道她的母亲的活动碎片。 虚设:

Interface IGetNumber { int getNum();}

class MyActivity : IGetNumber

class MyFragment {
          IGetNumber mNumGetter;

         onAttach(activity){
                If activity is an IGetNumber {
                           mNumGetter = (IGetNumber)activity;
                 }
        }

        any method(){
                If mNumGetter is valid {
                         Call mNumGetter.getNum();
                 }


}