I Get the Error "Unreachable Statement" Return in Android

I get the error Unreachable statement return in android

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

Unreachable Statement

Reorder onCreateView to look like this;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_picture_fragment,container,false);
topMemeText = (TextView) view.findViewById(R.id.topMemeText);
bottomMemeText = (TextView) view.findViewById(R.id.bottomMemeText);
return view;
}

Any code can't be executed after return statement in fuction. Remember then that it should be in last line of function.

It's showing unreachable statement error

you have two type of error:

1)return super (return super.onContextItemSelected(item)) , it means that you return your method at first line so lines below not execute.

2)wrong comparing String (item.getTitle()=="Hi")

correct code should be like this:

@Override
public boolean onContextItemSelected(MenuItem item) {
//return super.onContextItemSelected(item);//remove this line
if(item.getTitle().equal("Hi")){// also maybe you want to check not null for item.getTitle()
Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
}
return true;
}

Android Studio local variable says Unreachable statement Error

As mentioned in commnets, return keyword is used to exit the method. From there onwards execution will stop. So the code below return statement will not be executed and hence will become unreachable.

So put return view; as the last statement of the method

Android Studio Unreachable Statement

You are return immediately without assigning the inflated view to mRootView making the following statements not reachable

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

should be

mRootView =  inflater.inflate(R.layout.activity_analysis, container, false);

Unreachable statement for a function in a fragment

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_home,container,false);

getLocationPermision();
}

getLocationPermision(); is called AFTER your return statement. Making it unreachable.

Move it before the return statement.

I have a Error: error: unreachable statement Error:error: missing return statement

In onOptionsItemSelected() after 1st line if() is unreachable code pls use below.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}else{
return false;
}
}

Unreachable statement in onCreateView()

It's because of calling it after the return statement which means no other code lines will be executed. Do it before the return statement:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
loaderManager = getLoaderManager();
loaderManager.initLoader(1, null, this);

return super.onCreateView(inflater, container, savedInstanceState);
}


Related Topics



Leave a reply



Submit