gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:21:18 24 4
gpt4 key购买 nike

我对 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com