0

I am trying to integrate Facebook in my app. I want the user of my app to have the feature of sharing whats on his mind on his facebook wall. attached here is my code along with the log file. I have declared everything in my manifest as well. I dont know why am i getting java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.integration/com.integration.FBSharing}: java.lang.NullPointerException

10-29 14:53:58.344: E/AndroidRuntime(6459): FATAL EXCEPTION: main 10-29 14:53:58.344: E/AndroidRuntime(6459): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.integration/com.integration.FBSharing}: java.lang.NullPointerException 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891) 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.app.ActivityThread.access$600(ActivityThread.java:127) 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.os.Handler.dispatchMessage(Handler.java:99) 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.os.Looper.loop(Looper.java:137) 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.app.ActivityThread.main(ActivityThread.java:4441) 10-29 14:53:58.344: E/AndroidRuntime(6459): at java.lang.reflect.Method.invokeNative(Native Method) 10-29 14:53:58.344: E/AndroidRuntime(6459): at java.lang.reflect.Method.invoke(Method.java:511) 10-29 14:53:58.344: E/AndroidRuntime(6459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 10-29 14:53:58.344: E/AndroidRuntime(6459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 10-29 14:53:58.344: E/AndroidRuntime(6459): at dalvik.system.NativeStart.main(Native Method) 10-29 14:53:58.344: E/AndroidRuntime(6459): Caused by: java.lang.NullPointerException 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.content.ContextWrapper.getResources(ContextWrapper.java:81) 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.content.Context.getString(Context.java:286) 10-29 14:53:58.344: E/AndroidRuntime(6459): at com.integration.FBSharing.(FBSharing.java:34) 10-29 14:53:58.344: E/AndroidRuntime(6459): at java.lang.Class.newInstanceImpl(Native Method) 10-29 14:53:58.344: E/AndroidRuntime(6459): at java.lang.Class.newInstance(Class.java:1319) 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 10-29 14:53:58.344: E/AndroidRuntime(6459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)

public class FBSharing extends Activity {

// Your Facebook APP ID
 String APP_ID = getString(R.string.APP_ID);

// Instance of Facebook Class
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;

private SharedPreferences mPrefs;

// Buttons
Button btnFbLogin;
Button btnFbGetProfile;
Button btnPostToWall;
Button btnShowAccessTokens;


@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fblogin);

    btnFbLogin = (Button) findViewById(R.id.btn_fblogin);
    btnFbGetProfile = (Button) findViewById(R.id.btn_get_profile);
    btnPostToWall = (Button) findViewById(R.id.btn_fb_post_to_wall);
    btnShowAccessTokens = (Button) findViewById(R.id.btn_show_access_tokens);

    mAsyncRunner = new AsyncFacebookRunner(facebook);

    /**
     * Login button Click event
     * */
    btnFbLogin.setOnClickListener(new View.OnClickListener() 
    {

        public void onClick(View v) 
        {
            Log.d("Image Button", "button Clicked");
            loginToFacebook();
        }
    });

    /**
     * Getting facebook Profile info
     * */
    btnFbGetProfile.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            getProfileInformation();
        }
    });

    /**
     * Posting to Facebook Wall
     * */
    btnPostToWall.setOnClickListener(new View.OnClickListener() 
    {

        public void onClick(View v) 
        {
            postToWall();
        }
    });

    /**
     * Showing Access Tokens
     * */
    btnShowAccessTokens.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            showAccessTokens();
        }
    });

}

/**
 * Function to login into facebook
 * */
public void loginToFacebook() {

    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);

        btnFbLogin.setVisibility(View.INVISIBLE);

        Log.d("FB Sessions", "" + facebook.isSessionValid());
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {

                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();

                        // Making Login button invisible
                        btnFbLogin.setVisibility(View.INVISIBLE);

                    }

                    public void onError(DialogError error) {
                        // Function to handle error

                    }

                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors

                    }

                });
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    facebook.authorizeCallback(requestCode, resultCode, data);
}


/**
 * Get Profile information by making request to Facebook Graph API
 * */
public void getProfileInformation()
{
    mAsyncRunner.request("me", new RequestListener() 
    {
        public void onComplete(String response, Object state) 
        {
            Log.d("Profile", response);
            String json = response;
            try 
            {
                // Facebook Profile JSON data
                JSONObject profile = new JSONObject(json);

                // getting name of the user
                final String name = profile.getString("name");

                // getting email of the user
                final String email = profile.getString("email");

                runOnUiThread(new Runnable() 
                {

                    public void run() 
                    {
                        Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
                    }

                });//end of runnable


            } //end of try
            catch (JSONException e) 
            {
                e.printStackTrace();
            }
        }// end of onComplete

        public void onIOException(IOException e, Object state) 
        {
        }

        public void onFileNotFoundException(FileNotFoundException e,
                Object state) 
        {
        }

        public void onMalformedURLException(MalformedURLException e,
                Object state) 
        {
        }

        public void onFacebookError(FacebookError e, Object state) 
        {
        }
    });// end of request
}// end of getProfile

/**
 * Function to post to facebook wall
 * */
public void postToWall() {
    // post on user's wall.
    Bundle params = new Bundle();
    params.putString("name","ICE App");
    params.putString("caption", "Bestbuy Deal for SONY Action Cam");
    params.putString("description", "Checkout SONY ICE For exciting deals!!");
    params.putString("link", "http://www.sony.com");
    params.putString("picture", "R.drawable.sony");

    facebook.dialog(this, "feed",params, new DialogListener() {

        public void onFacebookError(FacebookError e) {
        }

        public void onError(DialogError e) {
        }

        public void onComplete(Bundle values) {
        }

        public void onCancel() {
        }
    });

}

/**
 * Function to show Access Tokens
 * */
public void showAccessTokens() {
    String access_token = facebook.getAccessToken();

    Toast.makeText(getApplicationContext(),
            "Access Token: " + access_token, Toast.LENGTH_LONG).show();
}

/**
 * Function to Logout user from Facebook
 * */
public void logoutFromFacebook() {
    mAsyncRunner.logout(this, new RequestListener() {
        public void onComplete(String response, Object state) {
            Log.d("Logout from Facebook", response);
            if (Boolean.parseBoolean(response) == true) {
                runOnUiThread(new Runnable() {

                    public void run() {
                        // make Login button visible
                        btnFbLogin.setVisibility(View.VISIBLE);

                        // making all remaining buttons invisible

                        btnPostToWall.setVisibility(View.INVISIBLE);

                    }

                });

            }
        }

        public void onIOException(IOException e, Object state) {
        }

        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
        }

        public void onMalformedURLException(MalformedURLException e,
                Object state) {
        }

        public void onFacebookError(FacebookError e, Object state) {
        }
    });
}

}

4

2 回答 2

0

如果您阅读堆栈跟踪,它会说 FBSharing.java:34 存在空指针异常

10-29 14:53:58.344:E/AndroidRuntime(6459):在 com.integration.FBSharing.(FBSharing.java:34) 10-29

如果我正确计算了您的代码行数,则此行会导致您的应用程序崩溃:

btnFbLogin.setOnClickListener(new View.OnClickListener()

这意味着btnFbLogin为空。检查按钮的 id 并确保您确实获得了对按钮的引用。

于 2012-10-30T03:42:53.177 回答
-1

确保在函数中声明这些onCreate()

// Buttons
Button btnFbLogin;
Button btnFbGetProfile;
Button btnPostToWall;
Button btnShowAccessTokens;

我有同样的问题。我修好了它喜欢这个。

于 2017-01-12T16:49:06.410 回答