Fragment类中的局部变量

时间:2017-11-10 22:28:18

标签: android list android-fragments fragment-oncreateview

我有一个片段,我想在列表中显示值,所以我在活动食谱详细信息

中得到它
        IngredientsFragment ingredientsFrag = new IngredientsFragment();
    ingredientsFrag.setPosition(position);
    ingredientsFrag.setRecipes(MainActivity.receipes);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.FRGingredients, ingredientsFrag)
            .commit();

要注意的主要内容是 SetRecipes()方法,我传入 List ,它是MainActivity类中的静态List。

在成分片段中,我有成分片段

public class IngredientsFragment extends Fragment {
int position;
 List<Recipe> mRecipes;
String test;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.frag_recipe_ingredients, container, false);
    TextView ingredients = rootView.findViewById(R.id.ingredientsTxt);

    ingredients.setText(String.valueOf(mRecipes.get(0).getName()));
    return rootView;
}

public void setPosition(int position) {
    this.position = position;
}

public void setRecipes(List<Recipe> recipes) {
    test = String.valueOf(recipes.get(0).getName());
    mRecipes = recipes;
}
}

当我运行此代码时,这就是我得到的:

11-10 23:14:17.682 2812-2812/com.lemuel.lemubit.bakenow E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.lemuel.lemubit.bakenow, PID: 2812
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lemuel.lemubit.bakenow/com.lemuel.lemubit.bakenow.RecipeDetail}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2583)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5765)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
        Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.lemuel.lemubit.bakenow.RecipeDetail.onCreate(RecipeDetail.java:15)
        at android.app.Activity.performCreate(Activity.java:6360)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2530)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5765)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
        Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.lemuel.lemubit.bakenow.RecipeDetail.onCreate(RecipeDetail.java:15)
        at android.app.Activity.performCreate(Activity.java:6360)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2530)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5765)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
        Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
        at com.lemuel.lemubit.bakenow.Fragments.IngredientsFragment.onCreateView(IngredientsFragment.java:34)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239)
        at android.support.v4.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1507)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1303)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1502)
        at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1737)
        at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3512)
        at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
        at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:330)
        at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.lemuel.lemubit.bakenow.RecipeDetail.onCreate(RecipeDetail.java:15)
        at android.app.Activity.performCreate(Activity.java:6360)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2530)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5765)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

因此它无法识别变量 mRecipes ,但让我感到困惑的是,当我使用变量测试时,它就会出现问题,这意味着该列表实际上是传递下去的在方法中,但无论何时我尝试在onCreateView中访问它,它告诉我null错误。虽然如果我将其更改为

,我可以访问测试
ingredients.setText(test);

当我这样做时它起作用,这意味着OnCreateView看到变量 test 它也意味着最初传入方法的List不是null。

2 个答案:

答案 0 :(得分:1)

这不是你如何将数组或任何其他变量传递给活动中的片段。您必须通过以下方式将包含数组的包传递给您的片段:

Bundle bundle = new Bundle();
bundle.putStringArrayList("edttext", arraylist);
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

IngredientsFragment ingredientsFrag = new IngredientsFragment();
ingredientsFrag.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
            .replace(R.id.FRGingredients, ingredientsFrag)
            .commit();

确保您的POJO食谱实施Parcelable。

答案 1 :(得分:0)

  

引起:java.lang.NullPointerException:尝试调用接口方法&#39; java.lang.Object java.util.List.get(int)&#39;在null对象引用上           在com.lemuel.lemubit.bakenow.Fragments.IngredientsFragment.onCreateView(IngredientsFragment.java:34)

访问列表时,异常是清除NullPointerException。 你必须将你的列表放入Bundle并将其设置为你的片段的参数.. 然后在片段类中,您可以从onCreateView方法中的片段中获取此包。