我知道这个问题之前已被问过一百万次,但我已经尝试了所有可以找到的解决方案,但仍然无法正常工作。我试过打电话给#34;这个"对于上下文,我已经尝试了getActivity,我尝试过getContext(),但似乎没有什么似乎适用于这个片段。相同的代码在不同的片段中工作,这就是为什么我真的很困惑。任何帮助表示赞赏。
我的LoginFragment,我的问题可以在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);
}
}
编辑:代码格式
答案 0 :(得分:2)
您的阵列适配器属于类型审核
DECLARE LOCAL TEMPORARY TABLE ParentTable TABLE (Id INT IDENTITY(1,1) , [Name] VARCHAR (100));
DECLARE LOCAL TEMPORARY TABLE ChildTable TABLE (Id INT IDENTITY(1,1), ParentId INT NULL, [Value] VARCHAR (50));
INSERT INTO ParentTable VALUES ('Name 001'), ('Name 002'), ('Name 003');
INSERT INTO ChildTable VALUES (1, 'Val 01'), (2, 'Val 02'), (NULL, 'Val 03'), (4, 'Val 04');
SELECT C.*
FROM ChildTable C
LEFT JOIN ParentTable P ON P.Id = C.ParentId
WHERE P.Id IS NULL;
但是你的传球:
ArrayAdapter<Review> arrayAdapter
答案 1 :(得分:1)
您可能应该了解有关泛型和类型安全的更多信息。
您的15.8930000000
类型为reservations
,但您尝试创建错误的ArrayList<Reservation>
。将其更改为ArrayAdapter<Review>
。
答案 2 :(得分:0)
您应该传递一个$(SEPARATOR)
数组,而不是Review
数组。因此,请将适配器更改为Reservation
。
此外,当您在ArrayAdapter<Reservation>
方法中调用getActivity()
方法时,您的onCreateView()
尚未创建,这可能会导致错误。
要解决此问题,您应该将此行移至Activity
:
onActivityCreated()