-1

I am just learning programming and faced with such an error. I need to open activity ruki from home fragment. But when I run the emulator, my application crashes What am I doing wrong


    ImageButton ruki;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_home, null);
      final ImageButton imageButton = (ImageButton) view. findViewById(R.id.ruki);
      View.OnClickListener onClickListener = new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              switch (view.getId()){
                  case R.id.ruki:
                      Intent intent = new Intent( HomeFragment.this.getActivity(), ruki_activity.class);
                      startActivity(intent);
                      break;
              }
          }
      };
      ruki.setOnClickListener(onClickListener);
      return inflater.inflate(R.layout.fragment_home, container, false);

    }
} 
gojim
  • 15
  • 3
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Dec 05 '21 at 11:05

2 Answers2

0

you are never initiating ImageButton ruki;, you are obtainging reference to final ImageButton imageButton, which isn't even used. ruki.setOnClickListener(onClickListener); will cause NullPointerException (ALWAYS post exception stacktrace)

instead of creating new variable

final ImageButton imageButton = (ImageButton) view. findViewById(R.id.ruki);

just attach result of findViewById to declared one

ruki = (ImageButton) view. findViewById(R.id.ruki);
snachmsm
  • 17,866
  • 3
  • 32
  • 74
0
public void onClick(View view) {
Intent intent= new Intent(getActivity(),UsingBackKey.class);
startActivity(intent);
}