java - 在具有多个按钮的 fragment 中启动 Activity

标签 java android android-fragments fragment

我对 Java 编程还比较陌生,我想打开一个带有多个不同 Activity 按钮的 fragment 。但是,我总是在以下位置收到错误(...已在...中定义):

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)

此外,在其他变体中它不起作用,并且出现更多错误。

这是我的 fragment 的完整代码:

public DashboardFragment() {

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);

    ImageButton button = (ImageButton) rootView.findViewById(R.id.stundenplanbtn);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), Stundenplan.class);
            startActivity(intent);


        }
    });

    return rootView;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);

    ImageButton button2 = (ImageButton) rootView.findViewById(R.id.vertretungsbtn);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), Vertretungsplan.class);
            startActivity(intent);


        }
    });

    return rootView;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);

    ImageButton button2 = (ImageButton) rootView.findViewById(R.id.essenbtn);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), Essen.class);
            startActivity(intent);


        }
    });

    return rootView;
}
}

最佳答案

你真正想要的是这样的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);

    ImageButton button = (ImageButton) rootView.findViewById(R.id.stundenplanbtn);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), Stundenplan.class);
            startActivity(intent);


        }
    });

    ImageButton button2 = (ImageButton) rootView.findViewById(R.id.vertretungsbtn);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Vertretungsplan.class);
                startActivity(intent);


            }
        });

    return rootView;

}

此外,在 Android 特定上下文中,您实际上并不需要进行所有 ImageButton 转换等,因为您没有使用 ImageButton 类的任何特定方法,而且它扩展您可以简单使用的View类:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);


    rootView.findViewById(R.id.stundenplanbtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Stundenplan.class);
                startActivity(intent);


            }
        });

    rootView.findViewById(R.id.vertretungsbtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), Vertretungsplan.class);
                    startActivity(intent);


                }
            });

 return rootView;

}

请注意,这里唯一的事情不是创建按钮变量/引用,因此这只是一个小小的改进。但如果你现在感到困惑,请忽略并使用第一个。

关于java - 在具有多个按钮的 fragment 中启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44637355/

相关文章:

java - 是什么原因造成的 (MongoDB) : PropertyReferenceException: No property findAll found for type

android - 如何在我的应用程序中集成 Google 镜头?

android - FloatingActionButton 在 API 19 中有奇怪的背景

android - 如何使 fragment 中的 RecyclerView 可点击?

java - 小程序 Servlet 通信

java - 没有参数的私有(private)构造函数的目的是什么?

java - 使用Spring+dbcp刷新数据源

android - 如何在 Gradle 中设置预处理器定义

android - FragmentActivity 操作栏选项菜单

android - 控制 android fragment 创建顺序