2015-09-15 142 views
0

我一直在尋找發佈圖像在facebook上的牆壁,因爲早晨和谷歌搜索它,得到關於張貼的許多信息,但對我來說沒有運氣。我可以發佈鏈接沒有任何錯誤,但一旦我嘗試圖像獲取NullPointer異常在Facebook牆上張貼圖像

我的按鈕點擊事件監聽器即時通訊調用此方法後。如果我取消鏈接參數,然後它會顯示一個對話框,詢問份額和取消,如果我把這個params.putByteArray("picture", data);代碼,我會得到下面的logcat錯誤

09-15 15:51:50.647: E/AndroidRuntime(9716): FATAL EXCEPTION: main 
09-15 15:51:50.647: E/AndroidRuntime(9716): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.m2fpremiun.dressup/com.m2fpremiun.dressup.FBProjectActivity}: java.lang.NullPointerException 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2189) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2216) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.app.ActivityThread.access$600(ActivityThread.java:149) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.os.Handler.dispatchMessage(Handler.java:99) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.os.Looper.loop(Looper.java:153) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.app.ActivityThread.main(ActivityThread.java:5000) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at java.lang.reflect.Method.invoke(Method.java:511) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at dalvik.system.NativeStart.main(Native Method) 
09-15 15:51:50.647: E/AndroidRuntime(9716): Caused by: java.lang.NullPointerException 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at libcore.net.UriCodec.encode(UriCodec.java:132) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at java.net.URLEncoder.encode(URLEncoder.java:50) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at com.facebook.android.Util.encodeUrl(Util.java:85) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at com.facebook.android.Facebook.dialog(Facebook.java:774) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at com.m2fpremiun.dressup.FBProjectActivity.postToWall(FBProjectActivity.java:436) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at com.m2fpremiun.dressup.FBProjectActivity.loginToFacebook(FBProjectActivity.java:288) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at com.m2fpremiun.dressup.FBProjectActivity.onCreate(FBProjectActivity.java:83) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.app.Activity.performCreate(Activity.java:5020) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153) 
09-15 15:51:50.647: E/AndroidRuntime(9716):  ... 11 more 

請人幫助我獲得解決這個問題。

public void postToWall() { 
    // post on user's wall. 
    byte[] data = null; 
    Bundle params = new Bundle(); 
     //postParams.putString("description","I'm playing JackPac and Scored "+hScore+" ! Can you beat my High Score?"); 

    // postParams.putString("link", "https://play.google.com/store/apps/details?id=com.mobi2fun.jackpac&hl=en"); 


     Bitmap bi = BitmapFactory.decodeFile("/storage/sdcard0/Game/test.png"); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream();    
     bi.compress(Bitmap.CompressFormat.PNG, 100, baos);    
     data = baos.toByteArray();     

     params.putString("caption", "Image Post"); 
     //params.putString("description","I'm playing XYZ Game and Scored "+hScore+" ! Can you beat my High Score?"); 
     //params.putString("link", "https://play.google.com/store/apps/details?id=xyz.package&hl=en"); 

     params.putString("method", "photos.upload"); 
     params.putByteArray("picture", data); 
     facebook.dialog(this, "feed",params, new DialogListener() { 

     @Override 
     public void onFacebookError(FacebookError e) { 
     } 

     @Override 
     public void onError(DialogError e) { 
     } 

     @Override 
     public void onComplete(Bundle values) { 
      logoutFromFacebook(); 
      try { 
       //facebook.logout(getApplicationContext()); 
       //facebook.setAccessExpires(0); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void onCancel() { 
      logoutFromFacebook(); 
      try { 
      // facebook.logout(getApplicationContext()); 
       mAsyncRunner.logout(getApplicationContext(),new RequestListener() { 

        @Override 
        public void onMalformedURLException(MalformedURLException e, Object state) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onIOException(IOException e, Object state) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onFileNotFoundException(FileNotFoundException e, Object state) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onFacebookError(FacebookError e, Object   state) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onComplete(String response, Object state) { 
         // TODO Auto-generated method stub 

        } 
       }); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 


    }); 

}

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

這些permisions我menifest文件給

我已經檢查這個環節,嘗試了所有codeBUT沒有運氣Facebook wall post with text and image Upload image to FaceBook from SDCard

+0

檢查這一行了java.lang.RuntimeException:無法啓動活動ComponentInfo {com.m2fpremiun.dressup/com.m2fpremiun.dressup.FBProjectActivity –

+1

是的,我檢查了它,這是此行上的空指針異常params.putByteArray(「picture」,data);如果我刪除這條線,每一件事都會運行良好,但我需要在Facebook牆上發佈圖像 –

回答

0

我使用這個代碼在Facebook牆上分享文字和圖片。其作品。這可能對你有幫助。試試這個。

從下面的代碼中顯示的oncreate方法調用facebooksharing方法。你可以從你想要的地方打電話給它。

CallbackManager callbackManager; 
 
private LoginManager manager; 
 

 
@Override 
 
\t protected void onCreate(Bundle savedInstanceState) { 
 
\t \t super.onCreate(savedInstanceState); 
 
\t \t FacebookSdk.sdkInitialize(this.getApplicationContext()); 
 
\t \t setContentView(R.layout.Your_layout); 
 
\t \t callbackManager = CallbackManager.Factory.create(); 
 

 
\t \t //call method for sharing post 
 
\t \t FaceBookSharing(); 
 
} 
 

 
public void FaceBookSharing() { 
 
List<String> permissionNeeds = Arrays.asList("publish_actions"); 
 
\t manager = LoginManager.getInstance(); 
 
\t manager.logInWithPublishPermissions(this, permissionNeeds); 
 

 
\t manager.registerCallback(callbackManager, 
 
\t \t new FacebookCallback<LoginResult>() { 
 
\t \t \t @Override 
 
\t \t \t public void onSuccess(LoginResult loginResult) { 
 
\t \t \t \t PostImage(); 
 
\t \t \t } 
 

 
\t \t \t @Override 
 
\t \t \t public void onCancel() { 
 
\t \t \t \t System.out.println("onCancel"); 
 
\t \t \t } 
 

 
\t \t \t @Override 
 
\t \t \t \t public void onError(FacebookException exception) { 
 
\t \t \t \t System.out.println("onError"); 
 
\t \t \t } 
 
\t }); 
 
} 
 
private void PostImage() { 
 
\t Bitmap image = BitmapFactory.decodeResource(getResources(), 
 
\t \t \t \t R.drawable.ic_launcher); 
 

 
\t SharePhoto photo = new SharePhoto.Builder().setBitmap(image) 
 
\t \t \t \t .setCaption("Hey this is Demo post!") 
 
\t \t \t \t .build(); 
 

 
\t SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(
 
\t \t \t \t photo).build(); 
 

 
\t ShareApi.share(content, null); 
 
\t Toast.makeText(this, "Succsesfully posted on your wall", Toast.LENGTH_LONG) 
 
\t .show(); \t 
 
}

+0

感謝您的回覆@哈維Sirja ..但是,我的代碼有什麼問題..如果我谷歌發佈圖像相同的代碼將顯示BUT我的代碼只是沒有運行。你可以用我的代碼檢查嗎? –