我在android中收到错误“Unreachable statement”返回

Posted

技术标签:

【中文标题】我在android中收到错误“Unreachable statement”返回【英文标题】:I get the error "Unreachable statement" return in android 【发布时间】:2015-03-22 01:28:59 【问题描述】:

为什么我会收到第 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

【问题讨论】:

【参考方案1】:

我们不会将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;


【讨论】:

问题已解决,但现在我在模拟器中显示此消息“不幸的是,AppnName 已停止”。 而且我不知道问题出在哪里 我在 logcat 中的错误为零,并且在代码中没有错误。我完全迷路了,我是 java 和 android 的新手。 当我尝试运行该项目时,我收到以下消息“不幸的是,AppnName 已停止”。 那么logcat中应该有东西【参考方案2】:

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);

【讨论】:

您好,欢迎来到 StackOverlfow。虽然代码 sn-p 可能会回答问题,但最好在代码中附上一些解释 - 请尝试添加一些解释。【参考方案3】:

你在它上面有一个回报。当你从一个方法返回时,它下面的任何东西都不会被执行。这意味着它下面的行永远不会被执行。

编译器正试图帮助您看到这一点。

【讨论】:

好的。我能做些什么呢?你有什么想法吗? @Edd_boy ,你需要掌握Java编程语言才能开发Android。

以上是关于我在android中收到错误“Unreachable statement”返回的主要内容,如果未能解决你的问题,请参考以下文章

为啥我在 Android Studio 中收到此错误“Expected resource of type raw”?

当我在android中使用Bitmap压缩图像时,我收到错误

为啥我在 Chrome 中运行时在 Android 中收到“无法为 m-section 设置远程视频描述发送参数,mid='0'”错误?

我在启用 android.useAndroidX 的情况下收到“AndroidX 依赖项”错误

为啥我在使用 ActionBar Sherlock 和旧版 android 版本时在 onCreate() 处收到错误? [关闭]

想要在默认的 android 抽屉上打印电子邮件但收到一些“Closure()”错误