处理程序是无法访问的语句

huangapple go评论78阅读模式
英文:

Handler is Unreachable Statement

问题

我正在创建一个应用程序,它从我的数据库(000webhost)进行连接,并且我正在使用这个[http url连接][1]。

以下是我的代码:

```java
public class RegisterFragment extends Fragment {

    public RegisterFragment() {
        // 必要的空公共构造函数
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 为此片段填充布局
        return inflater.inflate(R.layout.fragment_register, container, false);

        // 首先启动进度条(设置可见性为VISIBLE)
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                FetchData fetchData = new FetchData("https://projects.vishnusivadas.com/AdvancedHttpURLConnection/readTest.php");
                if (fetchData.startFetch()) {
                    if (fetchData.onComplete()) {
                        String result = fetchData.getResult();
                        // 结束进度条(将可见性设置为GONE)
                    }
                }
            }
        });
    }
}

而且它告诉我Handler部分是

>不可达语句

有人可以指出为什么会出现错误吗?
提前谢谢。


<details>
<summary>英文:</summary>

I am creating an app that connects from my database(000webhost) and I am using this
for the [http url connection][1] . 

And here&#39;s my code

public class RegisterFragment extends Fragment {

public RegisterFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_register, container, false);

    //Start ProgressBar first (Set visibility VISIBLE)
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            FetchData fetchData = new FetchData(&quot;https://projects.vishnusivadas.com/AdvancedHttpURLConnection/readTest.php&quot;);
            if (fetchData.startFetch()) {
                if (fetchData.onComplete()) {
                    String result = fetchData.getResult();
                    //End ProgressBar (Set visibility to GONE)

                }
            }
        }
    });
}

}


And it is telling me that the ```Handler``` part is

&gt;Unreachable Statement

Could someone point out why it is having an error.
Thank you in advance


  [1]: https://github.com/VishnuSivadasVS/Advanced-HttpURLConnection

</details>


# 答案1
**得分**: 1

你在 Handler 代码的上方有一个返回语句。

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

因此,在达到 Handler 之前,该函数将会返回。

解释:https://www.geeksforgeeks.org/return-keyword-java/

<details>
<summary>英文:</summary>

You have a return statement just above the Handler code.

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

Therefore the function will return before ever reaching the Handler.

Explaination: https://www.geeksforgeeks.org/return-keyword-java/

</details>



huangapple
  • 本文由 发表于 2020年10月21日 21:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64464699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定