简单Facebook库中的空指针异常到getPosts?

时间:2015-07-18 07:17:26

标签: java android facebook nullpointerexception android-simple-facebook

我导入了THIS LIBRARY以获取我的所有帖子,我设置了所有导入,因此,我已按照步骤操作并编写此代码,实际上我想在我的应用中显示我的网页的所有公开信息。就像一些应用或网站保持这看起来像Facebook的真实页面。在android中有可能吗?感谢。

package algonation.com.myapplication;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sromku.simple.fb.SimpleFacebook;
import com.sromku.simple.fb.actions.Cursor;
import com.sromku.simple.fb.entities.Post;
import com.sromku.simple.fb.listeners.OnPostsListener;

import java.util.List;


public class FacebookActivity extends ActionBarActivity {
private final static String EXAMPLE = "";
private String mAllPages = "";
OnPostsListener onPostsListener = new OnPostsListener() {
    @Override
    public void onComplete(List<Post> posts) {
        Log.i(EXAMPLE, "Number of posts = " + posts.size());
    }

/*
 * You can override other methods here:
 * onThinking(), onFail(String reason), onException(Throwable throwable)
 */
};

private TextView mResult;
private Button mGetButton;
private TextView mMore;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facebook);
    mResult = (TextView) findViewById(R.id.result);
    mMore = (TextView) findViewById(R.id.load_more);
    mMore.setPaintFlags(mMore.getPaint().getFlags() | Paint.UNDERLINE_TEXT_FLAG);
    mGetButton = (Button) findViewById(R.id.button);
    mGetButton.setText(EXAMPLE);
    mGetButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAllPages = "";
            mResult.setText(mAllPages);
            SimpleFacebook.getInstance().getPosts(new OnPostsListener() {
                @Override
                public void onThinking() {
                }

                @Override
                public void onException(Throwable throwable) {
                    mResult.setText(throwable.getMessage());
                }

                @Override
                public void onFail(String reason) {
                    mResult.setText(reason);
                }

                @Override
                public void onComplete(List<Post> response) {
                    // make the result more readable.
                    mAllPages += "<u>\u25B7\u25B7\u25B7 (paging) #" + getPageNum() + " \u25C1\u25C1\u25C1</u><br>";
                    mAllPages += com.sromku.simple.fb.utils.Utils.join(response.iterator(), "<br>", new com.sromku.simple.fb.utils.Utils.Process<Post>() {
                        @Override
                        public String process(Post post) {
                            return "\u25CF " + post.getMessage() == null || "null".equalsIgnoreCase(post.getMessage()) ? post.getId() : post.getMessage() + " \u25CF";
                        }
                    });
                    mAllPages += "<br>";
                    mResult.setText(Html.fromHtml(mAllPages));
                    // check if more pages exist
                    if (hasNext()) {
                        enableLoadMore(getCursor());
                    } else {
                        disableLoadMore();
                    }
                }
            });
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_facebook, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void enableLoadMore(final Cursor<List<Post>> cursor) {
    mMore.setVisibility(View.VISIBLE);
    mMore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            mAllPages += "<br>";
            cursor.next();
        }
    });
}

private void disableLoadMore() {
    mMore.setOnClickListener(null);
    mMore.setVisibility(View.INVISIBLE);
}
}

1 个答案:

答案 0 :(得分:1)

如果查看code of the library,您会看到以下注释:

/**
 * Get the instance of {@link com.sromku.simple.fb.SimpleFacebook}. <br>
 * <br>
 * <b>Important:</b> Use this method only after you initialized this library
 * or by: {@link #initialize(android.app.Activity)} or by {@link #getInstance(android.app.Activity)}
 *
 * @return The {@link com.sromku.simple.fb.SimpleFacebook} instance
 */

您应该使用SimpleFacebook.getInstance(FacebookActivity.this),因为您使用的方法将返回未初始化的引用(null),除非您之前已初始化它。

请花一些时间reading the documentation of the library - 这家伙显然已经花了一些时间在庞大的维基上工作。