开发者

Facebook Android SDK NullPointerException

I followed this tutorial online to set up the facebook sdk for my android app. it works up until the user logs in and then i get this error in logcat when it crashes

07-12 16:59:29.783: ERROR/AndroidRuntime(556): FATAL EXCEPTION: main
07-12 16:59:29.783: ERROR/AndroidRuntime(556): java.lang.NullPointerException
07-12 16:59:29.783: ERROR/AndroidRuntime(556):     at com.outfit.first.FBConnectionActivity$IDRequestListener$1.run(FBConnectionActivity.java:129)
07-12 16:59:29.783: ERROR/AndroidRuntime(556):     at android.os.Handler.handleCallback(Handler.java:587)
07-12 16:59:29.783: ERROR/AndroidRuntime(556):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-12 16:59:29.783: ERROR/AndroidRuntime(556):     at android.os.Looper.loop(Looper.java:123)
07-12 16:59:29.783: ERR开发者_开发技巧OR/AndroidRuntime(556):     at android.app.ActivityThread.main(ActivityThread.java:3839)
07-12 16:59:29.783: ERROR/AndroidRuntime(556):     at java.lang.reflect.Method.invokeNative(Native Method)
07-12 16:59:29.783: ERROR/AndroidRuntime(556):     at java.lang.reflect.Method.invoke(Method.java:507)
07-12 16:59:29.783: ERROR/AndroidRuntime(556):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-12 16:59:29.783: ERROR/AndroidRuntime(556):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-12 16:59:29.783: ERROR/AndroidRuntime(556):     at dalvik.system.NativeStart.main(Native Method)

My entire code is bellow and I starred the lines that logcat says called the error, it is this line:

 username.setText("Welcome: " + name+"\n ID: "+id);

Can anyone help me figure out what im doing wrong? I feel like im so close to it working.

    import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.Facebook.DialogListener;

public abstract class FBConnectionActivity extends Activity {
    public static final String TAG = "FACEBOOK";
    private Facebook mFacebook;
    public static final String APP_ID = "IDHERE";
    private AsyncFacebookRunner mAsyncRunner;
    private static final String[] PERMS = new String[] { "read_stream" };
    private SharedPreferences sharedPrefs;
    private Context mContext;

    private TextView username;
    private ProgressBar pb;

    public void setConnection() {
            mContext = this;
            mFacebook = new Facebook(APP_ID);
            mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    }

    public void getID() {
        if (isSession()) {
            Log.d(TAG, "sessionValid");
            mAsyncRunner.request("me", new IDRequestListener());
        } else {
            // no logged in, so relogin
            Log.d(TAG, "sessionNOTValid, relogin");
            mFacebook.authorize(this, PERMS, new LoginDialogListener());
        }
    }

    public void getID(TextView txtUserName, ProgressBar progbar) {
            username = txtUserName;
            pb = progbar;
            if (isSession()) {
                    Log.d(TAG, "sessionValid");
                    mAsyncRunner.request("me", new IDRequestListener());
            } else {
                    // no logged in, so relogin
                    Log.d(TAG, "sessionNOTValid, relogin");
                    mFacebook.authorize(this, PERMS, new LoginDialogListener());
            }
    }

    public boolean isSession() {
            sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
            String access_token = sharedPrefs.getString("access_token", "x");
            Long expires = sharedPrefs.getLong("access_expires", -1);
            Log.d(TAG, access_token);

            if (access_token != null && expires != -1) {
                    mFacebook.setAccessToken(access_token);
                    mFacebook.setAccessExpires(expires);
            }
            return mFacebook.isSessionValid();
    }

    private class LoginDialogListener implements DialogListener {

            @Override
            public void onComplete(Bundle values) {
                    Log.d(TAG, "LoginONComplete");
                    String token = mFacebook.getAccessToken();
                    long token_expires = mFacebook.getAccessExpires();
                    Log.d(TAG, "AccessToken: " + token);
                    Log.d(TAG, "AccessExpires: " + token_expires);
                    sharedPrefs = PreferenceManager
                                    .getDefaultSharedPreferences(mContext);
                    sharedPrefs.edit().putLong("access_expires", token_expires)
                                    .commit();
                    sharedPrefs.edit().putString("access_token", token).commit();
                    mAsyncRunner.request("me", new IDRequestListener());
            }

            @Override
            public void onFacebookError(FacebookError e) {
                    Log.d(TAG, "FacebookError: " + e.getMessage());
            }

            @Override
            public void onError(DialogError e) {
                    Log.d(TAG, "Error: " + e.getMessage());
            }

            @Override
            public void onCancel() {
                    Log.d(TAG, "OnCancel");
            }
    }

    private class IDRequestListener implements RequestListener {

            @Override
            public void onComplete(String response, Object state) {
                    try {
                            Log.d(TAG, "IDRequestONComplete");
                            Log.d(TAG, "Response: " + response.toString());
                            JSONObject json = Util.parseJson(response);

                            final String id = json.getString("id");
                            final String name = json.getString("name");
                            FBConnectionActivity.this.runOnUiThread(new Runnable() {
                                    public void run() {
                                    //!!!line 129!!!        username.setText("Welcome: " + name+"\n ID: "+id);
                                            pb.setVisibility(ProgressBar.GONE);

                                    }
                            });
                    } catch (JSONException e) {
                            Log.d(TAG, "JSONException: " + e.getMessage());
                    } catch (FacebookError e) {
                            Log.d(TAG, "FacebookError: " + e.getMessage());
                    }
            }

            @Override
            public void onIOException(IOException e, Object state) {
                    Log.d(TAG, "IOException: " + e.getMessage());
            }

            @Override
            public void onFileNotFoundException(FileNotFoundException e,
                            Object state) {
                    Log.d(TAG, "FileNotFoundException: " + e.getMessage());
            }

            @Override
            public void onMalformedURLException(MalformedURLException e,
                            Object state) {
                    Log.d(TAG, "MalformedURLException: " + e.getMessage());
            }

            @Override
            public void onFacebookError(FacebookError e, Object state) {
                    Log.d(TAG, "FacebookError: " + e.getMessage());
            }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            mFacebook.authorizeCallback(requestCode, resultCode, data);
    }
}

EDIT: So this is the code in my activity that starts facebook from a button click:

ImageButton combine = (ImageButton) findViewById(R.id.CompressImg);
        combine.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setConnection();
                getID();
                //createoneimage("final.png");          
            }
            });

And this is my main.java class which im honestly not sure what it does:

public class main extends FBConnectionActivity {
    private TextView txtUserName;
    private ProgressBar pbLogin;
    private Button btnLogin;

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

    txtUserName = (TextView) findViewById(R.id.textFacebook);
    pbLogin = (ProgressBar) findViewById(R.id.progressLogin);
    btnLogin = (Button) findViewById(R.id.buttonLogin);
            btnLogin.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                            pbLogin.setVisibility(ProgressBar.VISIBLE);
                            setConnection();
                            getID(txtUserName, pbLogin);
                    }
            });
}
}

And finally this is my mainfb.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent" android:id="@+id/textFacebook"
                android:gravity="center_horizontal" android:layout_height="wrap_content"
                android:text="@string/welcome" android:layout_alignParentTop="true" />
        <Button android:text="@string/enter" android:id="@+id/buttonLogin"
                android:layout_below="@+id/textFacebook"
                android:layout_centerHorizontal="true" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_marginTop="30dip"></Button>
        <ProgressBar android:id="@+id/progressLogin"
                android:layout_centerInParent="true" android:layout_width="wrap_content"
                android:visibility="gone" android:layout_height="wrap_content"></ProgressBar>
</RelativeLayout>

EDIT 2: Since ive been working with this code ive realized that every time i use this code the first time i log in the app crashes and then the second time it logs in fine once it already has the fb user information saved. Bellow is the logcat error i get when it crashes. I'm not sure if its really working the second time because i dont get any errors but when i try to post to my fb wall it get errors then.

07-16 21:46:16.720: ERROR/AndroidRuntime(818): FATAL EXCEPTION: main
07-16 21:46:16.720: ERROR/AndroidRuntime(818): java.lang.NullPointerException
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at com.outfit.first.FBConnectionActivity$IDRequestListener$1.run(FBConnectionActivity.java:169)
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at android.os.Handler.handleCallback(Handler.java:587)
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at android.os.Looper.loop(Looper.java:123)
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at android.app.ActivityThread.main(ActivityThread.java:3839)
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at java.lang.reflect.Method.invokeNative(Native Method)
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at java.lang.reflect.Method.invoke(Method.java:507)
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-16 21:46:16.720: ERROR/AndroidRuntime(818):     at dalvik.system.NativeStart.main(Native Method)


Ok so i think your issue is the that your getID() method uses the IDRequestListener which then is trying to set the username and id in a textview which is null because your calling getID() without passing the textview or progressbar as a parameter. So you have two options, firstly you could just add this function to your FBConnectionActivity class:

public void login(){
    if (!isSession()) {
        // no logged in, so relogin
        Log.d(TAG, "login");
        mFacebook.authorize(this, PERMS, new LoginDialogListener());
    }
}

Then rather than calling getID() just call login() - this way you will skip the IDRequestListener and hence there will be no null pointers. Or you could simply put a try/catch around the offending code:

try{
    username.setText("Welcome: " + name+"\n ID: "+id);
    pb.setVisibility(ProgressBar.GONE);
}catch(Exception e){
    e.printStackTrace();
}

which would catch the exception and continue.

EDIT: infact looking through my answer i noticed that loginDialogListener uses the IDRequestListener so you either need to remove the calls to set the text view and progressbar or put the try/catch in!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜