问题
I know this question has been asked a million times before, but I have tried all of the solutions I can find, and still doesn't work. I have tried calling "this" for context, I have tried getActivity, I have tried getContext(), but nothing seems to work for this fragment in particular. The same code does work in a different fragment tho, which is why I am really confused. Any help appreciated.
My LoginFragment, my issue can be found in setReservations():
public class LoginFragment extends Fragment {
LoginButton loginButton;
TextView nameText;
ProfileTracker mProfileTracker;
DBHandler dbHandler;
Context context;
ArrayList<Reservation> reservations;
ListView lv;
Profile fbProfile;
CallbackManager callbackManager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login, container, false);
callbackManager = CallbackManager.Factory.create();
dbHandler = new DBHandler(getContext(), null, null, 1);
context = getActivity();
loginButton = (LoginButton) view.findViewById(R.id.login_button);
nameText = (TextView) view.findViewById(R.id.users_name);
loginButton.setReadPermissions("email");
setmProfileTracker();
mProfileTracker.startTracking();
// If using in a fragment
loginButton.setFragment(this);
// Other app specific specialization
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fbProfile = Profile.getCurrentProfile();
if (fbProfile != null)
{
updateUI();
}
getActivity().setTitle("My page");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}
private void setmProfileTracker()
{
mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
updateUI(); //this is the third piece of code I will discuss below
}
};
}
private void updateUI() {
boolean enableButtons = AccessToken.getCurrentAccessToken() != null;
if (fbProfile == null) {
fbProfile = Profile.getCurrentProfile();
Log.e("Profile", "null");
}
if (enableButtons && fbProfile != null) {
Log.e("Access Token", AccessToken.getCurrentAccessToken().toString());
nameText.setText(fbProfile.getName());
}
}
private void setReservations()
{
reservations = dbHandler.userReservationToArrayList(parseLong(fbProfile.getId()));
lv = (ListView) getView().findViewById(R.id.reservations);
ArrayAdapter<Review> arrayAdapter = new ArrayAdapter<Review>(
context,
android.R.layout.simple_list_item_1,
reservations);
lv.setAdapter(arrayAdapter);
}
}
EDIT: Code format
回答1:
You probably should learn more about generics and type safety.
Your reservations
has type ArrayList<Reservation>
but you try to create ArrayAdapter<Review>
which is wrong. Change it to ArrayAdapter<Reservation>
.
回答2:
Your Array Adapter is of Type Review
ArrayAdapter<Review> arrayAdapter
But Your passing :
ArrayList<Reservation> reservations
Change it to ArrayAdapter<Reservation>
回答3:
You should pass an array of Review
not an array of Reservation
. So change your adapter to ArrayAdapter<Reservation>
.
Also, at the moment you call the getActivity()
method inside the onCreateView()
method your Activity
is not created yet, which can cause an error.
To solve this issue, you should to move this line to your onActivityCreated()
:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity();
}
来源:https://stackoverflow.com/questions/44178693/cannot-resolve-constructor-arrayadapterandroid-content-context-int-java-util