私はリストから値を表示したいの断片を持っているので、私はそこまでの主なものは、メモを取るためにローカル変数
IngredientsFragment ingredientsFrag = new IngredientsFragment();
ingredientsFrag.setPosition(position);
ingredientsFrag.setRecipes(MainActivity.receipes);
getSupportFragmentManager().beginTransaction()
.replace(R.id.FRGingredients, ingredientsFrag)
.commit();
活動レシピの詳細にこのようにそれを得ますSetRecipes()メソッド、私はリストで渡しています、それはMainActivityクラスの静的リストです。私は成分フラグメントでこれを持っている成分フラグメントで
:
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が変数テストを見ていることを意味する、働きますヌル。 java.lang.NullPointerExceptionが:によって引き起こさ