2012-06-27 25 views
0

嗨我已經集成Facebook到我的Android應用程序。下面是代碼:我的mfacebook對象是否已初始化?

public class Example extends Activity { 


public static final String APP_ID = "My App ID";//i have placed my app id here 

private LoginButton mLoginButton; 
private TextView mText; 
private Button mPostButton; 


private Facebook mFacebook; 
private AsyncFacebookRunner mAsyncRunner; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    if (APP_ID == null) { 
     Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " + 
       "specified before running this example: see Example.java"); 
    } 

    setContentView(R.layout.main); 
    mLoginButton = (LoginButton) findViewById(R.id.login); 
    mText = (TextView) Example.this.findViewById(R.id.txt); 

    mPostButton = (Button) findViewById(R.id.postButton); 

    **mFacebook = new Facebook(APP_ID);**//this is where i get a Null Pointer Exception 
    mAsyncRunner = new AsyncFacebookRunner(mFacebook); 

    SessionStore.restore(mFacebook, this); 
    SessionEvents.addAuthListener(new SampleAuthListener()); 
    SessionEvents.addLogoutListener(new SampleLogoutListener()); 
    mLoginButton.init(this, mFacebook); 



    mPostButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      mFacebook.dialog(Example.this, "feed", 
        new SampleDialogListener()); 
     } 
    }); 
    mPostButton.setVisibility(mFacebook.isSessionValid() ? 
      View.VISIBLE : 
      View.INVISIBLE); 
} 

當我運行應用程序,我在行得到一個空指針異常:
mfacebook =新的Facebook(APP_ID);

這是我的logcat

06-27 15:01:29.424:ERROR/AndroidRuntime(646):致命異常:主要 06-27 15:01:29.424:ERROR/AndroidRuntime(646 ):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.sardarjijokes.ncpl/com.sardarjijokes.ncpl.Example}:java.lang.NullPointerException 06-27 15:01:29.424:ERROR/AndroidRuntime(646) :at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 06-27 15:01:29.424:ERROR/AndroidRuntime(646):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06 -27 15:01:29.424:錯誤/ AndroidRuntime(646):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 06-27 15:01:29.424:錯誤/ AndroidRuntime(646):at android.app .ActivityThread $ H.handleMessage(ActivityThread.java:2033) 06-27 15:01:29.424:ERROR/AndroidRuntime(646):at android.os.Handler.dispatchMessage(Handler.java:99) 06-27 15 :01:29.424:錯誤/ AndroidRuntime(646):在android.os.Looper.loop(Looper.java:123) 06-27 15:01:29.424:錯誤/ AndroidRuntime(646):at android.app.ActivityThread .main(ActivityThread.java:4627) 06-27 15:01:29.424:ERROR/AndroidRuntime(646):at java.lang.reflect.Method.invokeNative(Native Method) 06-27 15:01:29.424: ERROR/AndroidRuntime(646):at java.lang.reflect.Method.invoke(Method.java:521) 06-27 15:01:29.424:ERROR/AndroidRuntim e(646):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 06-27 15:01:29.424:ERROR/AndroidRuntime(646):at com.android.internal。 os.ZygoteInit.main(ZygoteInit.java:626) 06-27 15:01:29.424:ERROR/AndroidRuntime(646):at dalvik.system.NativeStart.main(Native Method) 06-27 15:01:29.424 :錯誤/ AndroidRuntime(646):導致:java.lang.NullPointerException 06-27 15:01:29.424:錯誤/ AndroidRuntime(646):at com.sardarjijokes.ncpl.Example.onCreate(Example.java:85) 06-27 15:01:29.424:ERROR/AndroidRuntime(646):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 06-27 15:01:29.424:ERROR/AndroidRuntime(646):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

請提供建議。

+0

你確定APP_ID不爲空意味着你沒有得到Util.showAlert。如果是,那麼請日誌貓......... –

+0

這裏是我的建議:提供logcat輸出 –

+0

添加logcat..example.java第85行是我在上面的代碼中突出顯示的行。 – hemanth

回答

0

嘗試以下操作:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
setContentView(R.layout.main); //set Activity layout here 
    if (APP_ID == null) { 
     Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " + 
       "specified before running this example: see Example.java"); 
    } 


    mLoginButton = (LoginButton) findViewById(R.id.login); 
    mText = (TextView)findViewById(R.id.txt); 

    mPostButton = (Button) findViewById(R.id.postButton); 

    **mFacebook = new Facebook(APP_ID);**//this is where i get a Null Pointer Exception 
    mAsyncRunner = new AsyncFacebookRunner(mFacebook); 
+0

我不好意思是指不同的佈局。 – hemanth

+0

@hemanth:如果它真的幫助你,你可以將它作爲答案 –

0

我不明白你的代碼,如果APP_ID爲null顯示警告信息,但讓其餘代碼得到執行。

它不應該是:

if (APP_ID == null) { 
    Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " + "specified before running this example: see Example.java"); 
    return; 
}