I get the error “Unreachable statement” return in android

前端 未结 3 1305
余生分开走
余生分开走 2020-11-28 16:48

Why do I get the error that line 92 is an unreachable statement? The error is in this line:

final RadioButton r1 =         


        
相关标签:
3条回答
  • 2020-11-28 17:04

    We don't put return statement above any other statement unless that return is under any conditional statement. If we do that then all the statements below that would never get executed (means it would become unreachable under all circumstances) which causes the error you are getting.

    Do it like this

    public class TabFragmentA extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        RelativeLayout rootView = (RelativeLayout) inflater.inflate(R.layout.tab_layout_a, container, false);
    
    
        final RadioButton r1 = (RadioButton) rootView.findViewById(R.id.radio1);
        final RadioButton r2 = (RadioButton) rootView.findViewById(R.id.radio2);
    
        final ImageView iv1 = (ImageView) rootView.findViewById(R.id.iv1);
        final ImageView iv2 = (ImageView) rootView.findViewById(R.id.iv2);
    
        iv1.setVisibility(View.INVISIBLE);
        iv2.setVisibility(View.INVISIBLE);
    
        r1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(r1.isChecked())
                {
                    r2.setChecked(false);
                    iv2.setVisibility(View.INVISIBLE);
                    iv1.setVisibility(View.VISIBLE);
                }
            }
        });
    
        r2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(r2.isChecked())
                {
                    r1.setChecked(false);
                    iv1.setVisibility(View.INVISIBLE);
                    iv2.setVisibility(View.VISIBLE);
                }
            }
        });
    return rootView;
    }
    }
    
    0 讨论(0)
  • 2020-11-28 17:07

    The return statement should be at the end:

    final RadioButton r1 = (RadioButton) getView().findViewById(R.id.radio1);  
    final RadioButton r2 = (RadioButton) getView().findViewById(R.id.radio2);
    
    return (RelativeLayout) inflater.inflate(R.layout.tab_layout_a, container, false);
    
    0 讨论(0)
  • 2020-11-28 17:18

    You have a return right above it. When you return from a method nothing below it will get executed. That means the line below it will never get executed.

    The compiler is trying to help you see this.

    0 讨论(0)
提交回复
热议问题