Facebook的Android SDK中的NullPointerException [英] Facebook Android SDK NullPointerException
问题描述
我跟着这个教程在网上建立了我的Android应用Facebook的SDK。它的工作原理,直到在用户登录,然后当它崩溃我得到的logcat这个错误
07-12 16:59:29.783:ERROR / AndroidRuntime(556):致命异常:主要
07-12 16:59:29.783:ERROR / AndroidRuntime(556):显示java.lang.NullPointerException
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在com.outfit.first.FBConnectionActivity $ IDRequestListener $ 1.run(FBConnectionActivity.java:129)
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在android.os.Handler.handleCallback(Handler.java:587)
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在android.os.Handler.dispatchMessage(Handler.java:92)
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在android.os.Looper.loop(Looper.java:123)
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在android.app.ActivityThread.main(ActivityThread.java:3839)
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在java.lang.reflect.Method.invokeNative(本机方法)
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在java.lang.reflect.Method.invoke(Method.java:507)
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-12 16:59:29.783:ERROR / AndroidRuntime(556):在dalvik.system.NativeStart.main(本机方法)
我的整个code是波纹管,我出演了logcat的说叫错误的线路,这是这一行:
username.setText(欢迎+姓名+\\ n ID:+身份证);
谁能帮我找出什么即时通讯做错了什么?我觉得我太接近它的工作。
进口java.io.FileNotFoundException;
进口java.io.IOException异常;
进口java.net.MalformedURLException;进口org.json.JSONException;
进口org.json.JSONObject;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.os.Bundle;
。进口的Android preference preferenceManager。
进口android.util.Log;
进口android.widget.ProgressBar;
进口android.widget.TextView;进口com.facebook.android.AsyncFacebookRunner;
进口com.facebook.android.DialogError;
进口com.facebook.android.Facebook;
进口com.facebook.android.FacebookError;
进口com.facebook.android.Util;
进口com.facebook.android.AsyncFacebookRunner.RequestListener;
进口com.facebook.android.Facebook.DialogListener;公共抽象类FBConnectionActivity延伸活动{
公共静态最后弦乐TAG =脸谱;
私人facebook mFacebook;
公共静态最后弦乐APP_ID =IDHERE;
私人AsyncFacebookRunner mAsyncRunner;
私有静态最后的String [] = PERMS新的String [] {read_stream};
私人共享preferences共享preFS;
私人语境mContext; 私人TextView的用户名;
私人进度PB; 公共无效setConnection(){
mContext =这一点;
mFacebook =新的Facebook(APP_ID);
mAsyncRunner =新AsyncFacebookRunner(mFacebook);
} 公共无效的getID(){
如果(isSession()){
Log.d(TAGsessionValid);
mAsyncRunner.request(我,新IDRequestListener());
}其他{
//没有登录,所以重新登录
Log.d(TAG,sessionNOTValid,重新登录);
mFacebook.authorize(此,烫发,新LoginDialogListener());
}
} 公共无效的getID(TextView的txtUserName,进度progbar){
用户名= txtUserName;
PB = progbar;
如果(isSession()){
Log.d(TAGsessionValid);
mAsyncRunner.request(我,新IDRequestListener());
}其他{
//没有登录,所以重新登录
Log.d(TAG,sessionNOTValid,重新登录);
mFacebook.authorize(此,烫发,新LoginDialogListener());
}
} 公共布尔isSession(){
共享preFS = preferenceManager.getDefaultShared preferences(mContext);
串的access_token =共享prefs.getString(的access_token,×);
龙期满=共享prefs.getLong(access_expires,-1);
Log.d(TAG,的access_token); 如果(ACCESS_TOKEN = NULL&放大器;!&安培;!到期= -1){
mFacebook.setAccessToken(的access_token);
mFacebook.setAccessExpires(到期);
}
返回mFacebook.isSessionValid();
} 私有类LoginDialogListener实现DialogListener { @覆盖
公共无效的onComplete(束值){
Log.d(TAGLoginONComplete);
字符串标记= mFacebook.getAccessToken();
长token_expires = mFacebook.getAccessExpires();
Log.d(TAG的accessToken:+令牌);
Log.d(TAG,AccessExpires:+ token_expires);
共享preFS = preferenceManager
.getDefaultShared preferences(mContext);
共享prefs.edit()。putLong(access_expires,token_expires)
。承诺();
共享prefs.edit()putString(的access_token令牌).commit()。
mAsyncRunner.request(我,新IDRequestListener());
} @覆盖
公共无效onFacebookError(FacebookError E){
Log.d(TAGFacebookError:+ e.getMessage());
} @覆盖
公共无效onerror的(DialogError E){
Log.d(TAG,错误:+ e.getMessage());
} @覆盖
公共无效onCancel(){
Log.d(TAGOnCancel);
}
} 私有类IDRequestListener实现RequestListener { @覆盖
公共无效的onComplete(字符串响应,对象的状态){
尝试{
Log.d(TAGIDRequestONComplete);
Log.d(TAG,回应:+ response.toString());
JSONObject的JSON = Util.parseJson(响应); 最后弦乐ID = json.getString(ID);
最终的字符串名称= json.getString(名称);
FBConnectionActivity.this.runOnUiThread(新的Runnable(){
公共无效的run(){
// !!!线129! username.setText(欢迎+姓名+\\ n ID:+身份证);
pb.setVisibility(ProgressBar.GONE); }
});
}赶上(JSONException E){
Log.d(TAGJSONException:+ e.getMessage());
}赶上(FacebookError E){
Log.d(TAGFacebookError:+ e.getMessage());
}
} @覆盖
公共无效onIOException(IOException异常即对象的状态){
Log.d(TAGIOException异常:+ e.getMessage());
} @覆盖
公共无效onFileNotFoundException(FileNotFoundException异常E,
对象的状态){
Log.d(TAGFileNotFoundException异常:+ e.getMessage());
} @覆盖
公共无效onMalformedURLException(MalformedURLException的E,
对象的状态){
Log.d(TAGMalformedURLException的:+ e.getMessage());
} @覆盖
公共无效onFacebookError(FacebookError即对象的状态){
Log.d(TAGFacebookError:+ e.getMessage());
} } @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
mFacebook.authorizeCallback(要求code,结果code,数据);
}
}
编辑:
因此,这是在我的活动code,从一个按钮,点击开始Facebook的:
的ImageButton结合=(的ImageButton)findViewById(R.id.Com pressImg);
combine.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看视图){
setConnection();
的getID();
//createoneimage(\"final.png);
}
});
这是我main.java类即时通讯真的不知道它做什么:
公共类主要扩展FBConnectionActivity {
私人TextView的txtUserName;
私人进度pbLogin;
私人按钮btnLogin;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.mainfb); txtUserName =(的TextView)findViewById(R.id.textFacebook);
pbLogin =(进度)findViewById(R.id.progressLogin);
btnLogin =(按钮)findViewById(R.id.buttonLogin);
btnLogin.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
pbLogin.setVisibility(ProgressBar.VISIBLE);
setConnection();
的getID(txtUserName,pbLogin);
}
});
}
}
最后,这是我mainfb.xml文件
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直的android:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
<的TextView的android:layout_width =FILL_PARENT机器人:ID =@ + ID / textFacebook
机器人:重力=CENTER_HORIZONTAL机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/欢迎机器人:layout_alignParentTop =真/>
<按钮的android:文本=@字符串/输入机器人:ID =@ + ID / buttonLogin
机器人:layout_below =@ + ID / textFacebook
机器人:layout_centerHorizontal =真的android:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT机器人:layout_marginTop =30dip>< /按钮>
<进度机器人:ID =@ + ID / progressLogin
机器人:layout_centerInParent =真的android:layout_width =WRAP_CONTENT
机器人:知名度=水涨船高机器人:layout_height =WRAP_CONTENT>< /进度>
< / RelativeLayout的>
编辑2:
由于香港专业教育学院一直与这个code香港专业教育学院的工作意识到,每一次我用这个code我第一次登录应用程序崩溃,然后第二次是在精细记录,一旦它已经保存了FB用户信息。贝娄是logcat的错误我得到时崩溃。我不知道,如果它真的工作第二次,因为我没有得到任何错误,但是当我尝试发布到我的FB墙壁得到它的错误呢。
21 07-16:46:16.720:ERROR / AndroidRuntime(818):致命异常:主要
07-16 21:46:16.720:ERROR / AndroidRuntime(818):显示java.lang.NullPointerException
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在com.outfit.first.FBConnectionActivity $ IDRequestListener $ 1.run(FBConnectionActivity.java:169)
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在android.os.Handler.handleCallback(Handler.java:587)
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在android.os.Handler.dispatchMessage(Handler.java:92)
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在android.os.Looper.loop(Looper.java:123)
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在android.app.ActivityThread.main(ActivityThread.java:3839)
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在java.lang.reflect.Method.invokeNative(本机方法)
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在java.lang.reflect.Method.invoke(Method.java:507)
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-16 21:46:16.720:ERROR / AndroidRuntime(818):在dalvik.system.NativeStart.main(本机方法)
行,所以我觉得你的问题是,你的的getID()
方法使用 IDRequestListener
,然后试图设置一个TextView是null,因为您的电话的getID()
不传递的TextView或用户名和ID进度作为参数。所以,你有两个选择,一是你可以只添加此功能,您FBConnectionActivity类:
公共无效登录(){
如果(!isSession()){
//没有登录,所以重新登录
Log.d(TAG,登录);
mFacebook.authorize(此,烫发,新LoginDialogListener());
}
}
然后,而不是调用的getID()
就叫登录()
- 这种方式,您将跳过 IDRequestListener
,因此不会有任何空指针。或者你可以简单地把一个try / catch周围的违规code:
{尝试
username.setText(欢迎+姓名+\\ n ID:+身份证);
pb.setVisibility(ProgressBar.GONE);
}赶上(例外五){
e.printStackTrace();
}
这将捕获异常并继续。
编辑:逸岸翻翻我的答案我注意到, loginDialogListener
使用 IDRequestListener
让你无论是需要删除调用设置文本视图,进度或将try / catch语句中!
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: ERROR/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!
这篇关于Facebook的Android SDK中的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!