Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java类之间传递变量?_Java_Android - Fatal编程技术网

如何在java类之间传递变量?

如何在java类之间传递变量?,java,android,Java,Android,我试图在这些类之间传递两个变量getName和getImage的值 public class FruitsFragment extends Fragment { public FruitsFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedI

我试图在这些类之间传递两个变量getName和getImage的值

public class FruitsFragment extends Fragment {

    public FruitsFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.word_list, container, false);
        // Create a test list of vegs
        final ArrayList<Veg> vegs = new ArrayList<Veg>();
        vegs.add(new Veg("FELFEL", R.drawable.splash));
        vegs.add(new Veg("FELFEL", R.drawable.splash));
        vegs.add(new Veg("FELFEL", R.drawable.splash));
        vegs.add(new Veg("FELFEL", R.drawable.splash));
        vegs.add(new Veg("FELFEL", R.drawable.splash));
        vegs.add(new Veg("FELFEL", R.drawable.splash));
        vegs.add(new Veg("FELFEL", R.drawable.splash));
        vegs.add(new Veg("FELFEL", R.drawable.splash));

        // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
        // adapter knows how to create list items for each item in the list.
        VegAdapter adapter = new VegAdapter(getActivity(), vegs);

        // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
        // There should be a {@link ListView} with the view ID called list, which is declared in the
        // word_list.xml layout file.
        ListView listView = (ListView) rootView.findViewById(R.id.list);

        // Make the {@link ListView} use the {@link VegAdapter} created above, so that the
        // {@link ListView} will display list items for each {@link Word} in the list.
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                // Get the {@link Word} object at the given position the user clicked on
                Veg veg = vegs.get(position);
                FelfelActivity fel = new FelfelActivity();
                fel.setImage(veg.getImage());
                fel.setDes(veg.getName());
                Intent intent = new Intent(FruitsFragment.this.getActivity(), FelfelActivity.class);
                FruitsFragment.this.startActivity(intent);
            }
        });
        return rootView;
    }

}
而且它似乎不起作用,因为当我在手机上运行应用程序并点击列表中的任何元素时,它会引导我进入正确的活动,但没有图像或名称,只有一个空白背景


我想知道是否有一种方法可以传递getImage的值并将其设置为名为imageF的ImageView,以及将getName的值设置为名为des的TextView。

将详细信息传递给其他活动,以用于启动活动:

        Intent intent = new Intent(FruitsFragment.this.getActivity(), FelfelActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("image", image);
        FruitsFragment.this.startActivity(intent);
在下一个活动中访问该意图

String name = getIntent().getStringExtra("name");
String image = getIntent().getStringExtra("image");

欲了解更多信息,请查看标题为Extras的部分。

您是否尝试过做一些研究?我想我们每周可能会有10个标题完全相同的问题。
String name = getIntent().getStringExtra("name");
String image = getIntent().getStringExtra("image");