web-dev-qa-db-ja.com

「到達不能ステートメント」というエラーが返されますandroid

92行目が到達不能ステートメントであるというエラーが表示されるのはなぜですか?エラーは次の行にあります:

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

コード:

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エラー:

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
5
Edd_boy

returnが条件付きステートメントの下にない限り、他のステートメントの上にreturnステートメントを配置しません。そうすると、以下のすべてのステートメントが実行されることはなく(つまり、すべての状況で到達不能になる)、エラーが発生します。

このようにしてください

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;
}
}
18
Rohit5k2

Returnステートメントは最後にある必要があります。

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
user6754488

その真上にリターンがあります。メソッドから戻ると、その下には何も実行されません。つまり、その下の行は実行されません。

コンパイラは、これを確認できるように支援しようとしています。

0