Reputation: 3
I am still relatively new in programming with Java and I would like to open a fragment with several buttons different activities. However, I always get an error (... is already defined in ...) at the following location:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
Also in other variants it did not work and there were even more errors.
Here is the full code of my 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;
}
}
Upvotes: 0
Views: 41
Reputation: 4127
What you actually want is this:
@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;
}
Also, in the Android
specific context you don't really all the ImageButton casting etc. since you're not using any specific method of the ImageButton
class and since it extends
the View
class you can simply use:
@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;
}
Note the only thing here is not creating the button
variables/references, so it's just a small improvement. But if this is confusing you at the moment just ignore and use the first one.
Upvotes: 3