I get the error "Unreachable statement" return in android
Asked Answered
D

3

8

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

final RadioButton r1 = (RadioButton) getView().findViewById(R.id.radio1);

Code:

public class TabFragmentA extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        if (container == null) {
            return null;
        }

        return (RelativeLayout) inflater.inflate(R.layout.tab_layout_a, container, false);


        final RadioButton r1 = (RadioButton) getView().findViewById(R.id.radio1);  //the error is here
        final RadioButton r2 = (RadioButton) getView().findViewById(R.id.radio2);

        final ImageView iv1 = (ImageView) getView().findViewById(R.id.iv1); 
        final ImageView iv2 = (ImageView) getView().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);
                }
            }
        });
    }
}

Logcat error:

01-23 02:43:31.082    1903-1903/br.com.eddboytools.PreDim E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: br.com.eddboytools.PreDim, PID: 1903
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
            at br.com.eddboytools.PreDim.TabFragmentA.onCreateView(TabFragmentA.java:29)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
            at android.view.View.measure(View.java:17430)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-23 02:43:31.094    1903-1915/br.com.eddboytools.PreDim I/art﹕ Background partial concurrent mark sweep GC freed 261(11KB) AllocSpace objects, 0(0B) LOS objects, 27% free, 1328KB/1840KB, paused 20.856ms total 51.174ms
Dragonet answered 23/1, 2015 at 3:36 Comment(0)
S
29

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;
}
}
Seta answered 23/1, 2015 at 3:39 Comment(8)
The problem was solved, but now i have this message in the emulator "Unfortunately, AppnName has stopped".Dragonet
And i don't know where is the problemDragonet
i have zero errors in the logcat and no errors in the code. I'm completely lost, and i'm new in java and android.Dragonet
when i try to run the project, i have the following massage "Unfortunately, AppnName has stopped".Dragonet
then there should be something in logcatSeta
Eddboy you should mark this as the answer. He answered your question and now you are just asking for more helpCandiecandied
Line 29: final RadioButton r1 = (RadioButton) rootView.findViewById(R.id.radio1);Dragonet
Make sure all the id's you are getting are declared in the xml files, and thus make sure the gen folder also get cleaned and rebuild if you added and still get the same error.Pellikka
P
1

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);
Preparator answered 24/8, 2016 at 21:29 Comment(1)
Hello, and welcome to StackOverlfow. While the code snippet might answer the question, it is always a good idea to accompany the code with some explanation - please try to add some explanation.Valetudinary
A
0

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.

Antonyantonym answered 23/1, 2015 at 3:38 Comment(2)
Ok. What can i do to it? You have any idea?Dragonet
@Dragonet , you need to have a good grasp on Java programming language before you could develop Android.Pellikka

© 2022 - 2024 — McMap. All rights reserved.