FloFlo :
why the "view" in this lane is always red..
final TextView username = (TextView) view.findViewById(R.id.profile_username);
I always get this error like below:
"error: cannot find symbol variable view"
Sample code which I tried:
ProfileFragment class:
public class ProfileFragment extends Fragment {
MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
ImageView profilsettings = (ImageView) view.findViewById(R.id.profile_toolbar_image);
profilsettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog();
bottomSheetDialog.show(getFragmentManager(), bottomSheetDialog.getTag());
}
});
return view;
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("my_broadcast")){
final TextView username = (TextView) view.findViewById(R.id.profile_username);
username.setText(intent.getStringExtra("name"));
}
}
}
@Override
public void onResume() {
super.onResume();
getActivity().registerReceiver(myReceiver, new IntentFilter("my_broadcast"));
}
@Override
public void onPause() {
super.onPause();
getActivity().unregisterReceiver(myReceiver);
}
}
Can anyone help me what do I have to change?
Thanks a lot!
Jayanth :
It is happening because the variable view is not accessible to your BroadcastReceiver
. So the compiler is saying error: cannot find symbol variable view
To make view accessible to BroadcastReceiver you can follow @Amine answer or you can easily make view
asmember
variable.
Make view object as a member variable like below
public class ProfileFragment extends Fragment {
MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_profile, container, false);
ImageView profilsettings = (ImageView) view.findViewById(R.id.profile_toolbar_image);
profilsettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog();
bottomSheetDialog.show(getFragmentManager(), bottomSheetDialog.getTag());
}
});
return view;
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("my_broadcast")){
final TextView username = (TextView) view.findViewById(R.id.profile_username);
username.setText(intent.getStringExtra("name"));
}
}
}
@Override
public void onResume() {
super.onResume();
getActivity().registerReceiver(myReceiver, new IntentFilter("my_broadcast"));
}
@Override
public void onPause() {
super.onPause();
getActivity().unregisterReceiver(myReceiver);
}
}
Happy coding!!
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=303233&siteId=1