问题
My app is meant to allow users to take a picture and then upload it to their Facebook wall. The following code is very similar to many other examples of working code given on other SO questions pertaining to this exact same problem, yet still gives me the null pointer exception:
private void postOnWall()
{
final String response = "";
try
{
Bundle params = new Bundle();
params.putString("message", getMessage());
params.putByteArray("picture", byteArray);
mAsyncRunner.request(me/feed, params, "POST", new SampleUploadListener(),
null);
}
catch (Exception e)
{
e.printStackTrace();
}
}
I would simply like to get the image upload working before I can move on to adding the message with it. Any ideas?
EDIT: Here is the SampleUploadListener() that goes with the AsyncRunner(...) line:
public class SampleUploadListener extends BaseRequestListener {
public void onComplete(final String response, final Object state) {
try {
// process the response here: (executed in background thread)
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
final String src = json.getString("src");
// then post the processed result back to the UI thread
// if we do not do this, an runtime exception will be generated
// e.g. "CalledFromWrongThreadException: Only the original
// thread that created a view hierarchy can touch its views."
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
}
LogCat:
06-22 13:23:45.136: W/System.err(20667): java.lang.NullPointerException
06-22 13:23:45.136: W/System.err(20667): at com.uncc.cci.TrashPickup.TrashPickupActivity.postwall(TrashPickupActivity.java:475)
06-22 13:23:45.136: W/System.err(20667): at com.uncc.cci.TrashPickup.TrashPickupActivity$5$1.onClick(TrashPickupActivity.java:230)
06-22 13:23:45.140: W/System.err(20667): at android.view.View.performClick(View.java:3511)
06-22 13:23:45.140: W/System.err(20667): at android.view.View$PerformClick.run(View.java:14105)
06-22 13:23:45.140: W/System.err(20667): at android.os.Handler.handleCallback(Handler.java:605)
06-22 13:23:45.140: W/System.err(20667): at android.os.Handler.dispatchMessage(Handler.java:92)
06-22 13:23:45.140: W/System.err(20667): at android.os.Looper.loop(Looper.java:137)
06-22 13:23:45.140: W/System.err(20667): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-22 13:23:45.144: W/System.err(20667): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 13:23:45.144: W/System.err(20667): at java.lang.reflect.Method.invoke(Method.java:511)
06-22 13:23:45.144: W/System.err(20667): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-22 13:23:45.144: W/System.err(20667): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-22 13:23:45.148: W/System.err(20667): at dalvik.system.NativeStart.main(Native Method)
回答1:
When you have corrected whatever bug you have here, you will unfortunately discover that it is not possible at this time to post an image to your wall by uploading the actual image bytes directly to facebook. You can upload a photo to their photos gallery, but not to their wall. To post a photo to the wall it must already reside online and you will need to provide a link to where it lives along with the other data. If I'm mistaken then this is a very new development as I just encountered all this a few months ago and even filed a bug with Facebook which they rejected saying that "it's not a bug, it's how we intended things to be".
If you're ok with putting the photo in their gallery (if it's the only photo IN the gallery it will look almost exactly like a regular wall post) this is the way to do it: Android post picture to Facebook wall
回答2:
Should be:
private void postOnWall() {
Bundle params = new Bundle();
params.putString("message", "New picture");
params.putByteArray("source", byteArray);
mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener(), null);
}
You sent the path as null and that's probably why you got the null pointer exception.
Also according to the documentation the image parameter name is source
.
Edit
Try this to check if mAsyncRunner is null:
private void postOnWall() {
Bundle params = new Bundle();
params.putString("message", "New picture");
params.putByteArray("source", byteArray);
Log.d("Facebook-Example", mAsyncRunner == null ? "mAsyncRunner is null" : "mAsyncRunner not null");
mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener(), null);
}
Since the error is thrown in that line, it seems like the only possible thing.
来源:https://stackoverflow.com/questions/11143644/null-pointer-exception-when-posting-a-picture-to-facebook-wall-android