我有一个片段,我想在其中显示列表中的值,所以我在 Activity Recipe Detail中得到它
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,但让我感到困惑的是,当我使用变量test时它会工作,这意味着该列表实际上是在方法中传递的,但是每当我尝试在 onCreateView 中访问它时,它都会告诉我空错误。虽然如果我将其更改为,我可以访问测试
ingredients.setText(test);
当我这样做时它会起作用,这意味着 OnCreateView 看到变量测试也意味着传递给方法的 List 最初不是空的。