2014-01-11 83 views
1

我想讓Facebook進入我的應用程序。所以我試着用簡單的測試應用程序在用戶按登錄按鈕,他顯示他的名字和profilepicture。這裏是我的代碼facebook SDK 3.6 for android without fragments

public class login extends Activity { 

private GraphUser user; 
private LoginButton loginbut; 
TextView username; 
ProfilePictureView userpic; 

private UiLifecycleHelper uihelper; 
private Session.StatusCallback callback = new Session.StatusCallback() { 

    @Override 
    public void call(Session session, SessionState state, Exception exception) { 
     // TODO Auto-generated method stub 
     onsessionstatechange(session, state, exception); 
    } 
}; 

private void setuploginandprofilepic() 
{ 
    loginbut = (LoginButton) findViewById(R.id.login_button); 
    loginbut.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() { 

     @Override 
     public void onUserInfoFetched(GraphUser user) { 
      // TODO Auto-generated method stub 
      login.this.user = user; 
      updateprofile(); 
     } 
    }); 
} 


private void updateprofile() { 
    // T 

ODO Auto-generated method stub 
     TextView username = (TextView) findViewById(R.id.username); 
     ProfilePictureView userpic = (ProfilePictureView) findViewById(R.id.userpic); 
     userpic.setCropped(true); 

     Session session = Session.getActiveSession(); 
     boolean validsession = session!=null && session.isOpened(); 

     if(validsession && user != null) 
     { 
      makemerequest(session); 
     }else 
     { 
      userpic.setProfileId(null); 
      username.setText("Not There"); 
     } 
    } 



     private void makemerequest(Session session){ 
      Request request = Request.newMeRequest(session, new Request.GraphUserCallback() { 



      @Override 
      public void onCompleted(GraphUser user, Response response) { 
       // TODO Auto-generated method stub 
       userpic.setProfileId(user.getId()); 
       username.setText(user.getFirstName()); 
      } 
     }); 
     request.executeAsync(); 
    } 

    private void onsessionstatechange(Session session, SessionState state, Exception exception) 
    { 
     if(session.isOpened()) 
     { 
      Toast.makeText(this, "OPEN", Toast.LENGTH_SHORT).show(); 
     } 
     else if(session.isClosed()) 
     { 
      Toast.makeText(this, "CLOSED", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     uihelper = new UiLifecycleHelper(this, callback); 
     uihelper.onCreate(savedInstanceState); 
     setuploginandprofilepic(); 
     setContentView(R.layout.testlayout); 
//  ProfilePictureView pp = (ProfilePictureView) findViewById(R.id.selection_profile_pic); 
//  TextView tv = (TextView) findViewById(R.id.selection_user_name); 
//  Session session = new Session(getApplicationContext()); 
//  Session.setActiveSession(session); 
    } 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    uihelper.onActivityResult(requestCode, resultCode, data); 
} 

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    uihelper.onDestroy(); 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    uihelper.onPause(); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    uihelper.onResume(); 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    // TODO Auto-generated method stub 
    super.onSaveInstanceState(outState); 
    uihelper.onSaveInstanceState(outState); 
} 


} 

這裏是我的logcat:

01-09 18:03:56.800: I/dalvikvm(8234): Could not find method android.support.v4.content.LocalBroadcastManager.getInstance, referenced from method com.facebook.UiLifecycleHelper.<init> 
01-09 18:03:56.800: W/dalvikvm(8234): VFY: unable to resolve static method 222: Landroid/support/v4/content/LocalBroadcastManager;.getInstance (Landroid/content/Context;)Landroid/support/v4/content/LocalBroadcastManager; 
01-09 18:03:56.800: D/dalvikvm(8234): VFY: replacing opcode 0x71 at 0x0019 
01-09 18:03:56.820: I/dalvikvm(8234): Could not find method android.support.v4.content.LocalBroadcastManager.unregisterReceiver, referenced from method com.facebook.UiLifecycleHelper.onPause 
01-09 18:03:56.820: W/dalvikvm(8234): VFY: unable to resolve virtual method 225: Landroid/support/v4/content/LocalBroadcastManager;.unregisterReceiver (Landroid/content/BroadcastReceiver;)V 
01-09 18:03:56.820: D/dalvikvm(8234): VFY: replacing opcode 0x6e at 0x0004 
01-09 18:03:56.830: I/dalvikvm(8234): Could not find method android.support.v4.content.LocalBroadcastManager.registerReceiver, referenced from method com.facebook.UiLifecycleHelper.onResume 
01-09 18:03:56.830: W/dalvikvm(8234): VFY: unable to resolve virtual method 223: Landroid/support/v4/content/LocalBroadcastManager;.registerReceiver (Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)V 
01-09 18:03:56.830: D/dalvikvm(8234): VFY: replacing opcode 0x6e at 0x0032 
01-09 18:03:56.840: D/AndroidRuntime(8234): Shutting down VM 
01-09 18:03:56.840: W/dalvikvm(8234): threadid=1: thread exiting with uncaught exception (group=0x40a95228) 
01-09 18:03:56.840: E/AndroidRuntime(8234): FATAL EXCEPTION: main 
01-09 18:03:56.840: E/AndroidRuntime(8234): java.lang.NoClassDefFoundError: android.support.v4.content.LocalBroadcastManager 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at com.facebook.UiLifecycleHelper.<init>(UiLifecycleHelper.java:72) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at com.droidlearnx.hwtofb.login.onCreate(login.java:85) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at android.app.Activity.performCreate(Activity.java:4538) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at android.app.ActivityThread.access$600(ActivityThread.java:139) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at android.os.Looper.loop(Looper.java:156) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at android.app.ActivityThread.main(ActivityThread.java:4987) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
01-09 18:03:56.840: E/AndroidRuntime(8234):  at dalvik.system.NativeStart.main(Native Method) 
01-09 18:03:58.261: D/Process(8234): killProcess, pid=8234 
01-09 18:03:58.271: D/Process(8234): dalvik.system.VMStack.getThreadStackTrace(Native Method) 
01-09 18:03:58.271: D/Process(8234): java.lang.Thread.getStackTrace(Thread.java:599) 
01-09 18:03:58.271: D/Process(8234): android.os.Process.killProcess(Process.java:807) 
01-09 18:03:58.271: D/Process(8234): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104) 
01-09 18:03:58.281: D/Process(8234): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) 
01-09 18:03:58.281: D/Process(8234): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690) 
01-09 18:03:58.291: D/Process(8234): dalvik.system.NativeStart.main(Native Method) 
01-09 18:03:58.291: 

I/Process(8234): Sending signal. PID: 8234 SIG: 9 

我已經試過所有我能,我似乎繞過它。我是facebook新手,這是我第一次使用它。我試圖做到這一點,而不使用片段,因爲它在Facebook開發者頁面上。我是否需要爲此學習片段,或者我可以使用活動工作嗎?

感謝您的幫助。

+0

在您的updateProfile()方法中是'// T ODO自動生成的方法存根'在這裏或在您的代碼中的錯誤嗎? – Coderji

+0

'java.lang.NoClassDefFoundError:android.support.v4.content.LocalBroadcastManager'你有沒有添加android支持v4庫 –

+0

@coderji是否真的很重要,因爲它仍然評論.. nope仍然不工作 –

回答

0

第1步:添加Android的私人圖書館(項目屬性 - > Java構建路徑 - >在線訂單和出口)

第2步:清潔項目,然後再次運行它。

它會解決這個問題。