I want to make an app using bottom navigation view in Android studio. The main activity is as follows, and depending on the tab, I want to make a button or do various things on Fragmen, but I keep getting errors.
public class MainActivity extends AppCompatActivity {
private Fragment fragment;
private FragmentManager fragmentManager;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_recentcall:
break;
case R.id.navigation_contact:
break;
case R.id.navigation_keypad:
fragment = new KeypadFragment();
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content, fragment).commit();
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
As an example, the keypad fragment part was written in this way
public class KeypadFragment extends Fragment {
String input_num="";
Button btnCall,btnAdd,btnErase;
Button[] numbutons =new Button[12];
TextView number;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
View view=inflater.inflate(R.layout.fragment_keypad,null);
btnCall = (Button) findViewById(R.id.button_call);
btnAdd = (Button) findViewById(R.id.button_add);
btnErase = (Button) findViewById(R.id.button_erase);
number = (TextView) findViewById(R.id.textView_input_number);
return view;
}
}
There was an error in this part.
btnCall = (Button) findViewById(R.id.button_call);
Why isn't findViewById working? Of course, findViewById for other widgets doesn't work either.<
android android-studio
Since I declared a view in onCreateView, it worked in the form of view.findViewById!!
© 2024 OneMinuteCode. All rights reserved.