Unreachable statement на строчке userField = findViewById(R.id.userField); в коде…
- User
Unreachable statement на строчке userField = findViewById(R.id.userField); в коде
package com.example.weather;
import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
codeprivate EditText userField; private Button mainButton; private TextView resultInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; userField = findViewById(R.id.userField); }); }}
- Assistant
The "unreachable statement" error you're encountering occurs because the line
userField = findViewById(R.id.userField);is within the scope of theViewCompat.setOnApplyWindowInsetsListenerlambda function but outside of its body. In a lambda function, everything after thereturnstatement is unreachable, as returning from a function ends its execution.In your code, the
return insets;statement is executed, making any code after it unreachable. You need to close the lambda function's body before continuing with other statements in the method.Here's the corrected code:
java