我试图在我的应用程序中制作简单的按钮。我已经制作了一个可以使用的分享按钮,我想给帖子点个赞。
我想写这段代码:
public class MainFragment extends Fragment {
private UiLifecycleHelper uiHelper;
TextView tv1;
TextView tv2;
Button btn;
Button btnLike;
ProfilePictureView profilePictureView ;
String like="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
tv1=(TextView) view.findViewById(R.id.textView1);
tv2=(TextView) view.findViewById(R.id.textView2);
btn=(Button) view.findViewById(R.id.button1);
btnLike=(Button) view.findViewById(R.id.button2);
btn.setOnClickListener(shareListener);
btnLike.setOnClickListener(likeListener);
profilePictureView =(ProfilePictureView) view.findViewById(R.id.selection_profile_pic);
profilePictureView.setCropped(true);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status","public_profile"));
//authButton.setPublishPermissions(Arrays.asList("publish_action"));
authButton.setFragment(this);
return view;
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i("log in", "Logged in...");
makeMeRequest(session);
}
else if (state.isClosed()) {
Log.i("log out", "Logged out...");
tv1.setText("");
tv2.setText("");
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private void makeMeRequest(final Session session) {
// Make an API call to get user data and define a
// new callback to handle the response.
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
if (session == Session.getActiveSession()) {
if (user != null) {
// Set the id for the ProfilePictureView
profilePictureView.setProfileId(user.getId());
// view that in turn displays the profile picture.
tv1.setText(user.getFirstName().toString()+"");
// Set the Textview's text to the user's name.
tv2.setText(user.getLastName().toString());
}
}
if (response.getError() != null) {
// Handle errors, will do so later.
}
}
});
request.executeAsync();
}
private OnClickListener shareListener=new OnClickListener() {
@Override
public void onClick(View v) {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
publishFeedDialog();
Bundle params = new Bundle();
params.putString("object", "http://samples.ogp.me/<699966466709368>");
Request request = new Request(
Session.getActiveSession(),
"me/og.likes",
params,
HttpMethod.POST
);
Response response = request.executeAndWait();
// handle the response
}
else{
Toast.makeText(getActivity(), "you must log in first", 1500).show();
}
}
};
private OnClickListener likeListener=new OnClickListener() {
@Override
public void onClick(View v) {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
Request likeRequest = new Request(Session.getActiveSession(),"/{"+ like.toString() +"}/likes", null, HttpMethod.POST, new Request.Callback() {
@Override
public void onCompleted(Response response) {
Log.i("like", response.toString());
}
});
Request.executeBatchAndWait(likeRequest);
}
}
};
private void publishFeedDialog() {
Bundle params = new Bundle();
params.putString("name", "aaa");
params.putString("caption", "bbb");
params.putString("description", "ccc");
params.putString("link", "https://developers.facebook.com/android");
params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(getActivity(),
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(getActivity(),
"Posted story, id: "+postId,
Toast.LENGTH_SHORT).show();
like=postId;
} else {
// User clicked the Cancel button
Toast.makeText(getActivity().getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(getActivity().getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(getActivity().getApplicationContext(),
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}
})
.build();
feedDialog.show();
}
}
但什么也没有发生..我在 logcat 中得到这个错误:
{Response: responseCode: 200, graphObject: null, error: {HttpStatus: -1, errorCode: -1, errorType: null, errorMessage: java.lang.NullPointerException}, isFromCache:false}
有些人知道如何修复它并在我的应用程序上制作一个赞按钮?