I get the error “Unreachable statement” return in android

前端 未结 3 1330
余生分开走
余生分开走 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;
    }
    }
    

提交回复
热议问题