0

MessagesActivity.java

package org.example.fbapp;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;

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

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.AsyncFacebookRunner.RequestListener;

import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MessagesActivity extends ListActivity {

    // Your Facebook APP ID
    private static String APP_ID = "5496*******054";

    // JSON Node names
    private static final String TAG_DATA = "data";
    private static final String TAG_MESSAGE = "message";

    // data JSONArray
    JSONArray data = null;

    // Instance of Facebook Class
    @SuppressWarnings("deprecation")
    private Facebook facebook = new Facebook(APP_ID);
    @SuppressWarnings("deprecation")
    private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> messages = new ArrayList<HashMap<String, String>>();
    private ListView lv;

    ListView mylistview;
    ArrayList<String> array_months;
    ArrayAdapter<String> listAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.contacts_view);

        ListAdapter adapter = createAdapter();
        setListAdapter(adapter);
    }

    /**
     * Creates and returns a list adapter for the current list activity
     * 
     * @return
     */
    @SuppressWarnings("deprecation")
    protected ListAdapter createAdapter()
    {
        mAsyncRunner.request("203153109726651/feed", new RequestListener() {
            @Override
            public void onComplete(String response, Object state) {
                Log.d("GET POSTS", response);
                String json = response;

                try {

                    // Facebook Profile JSON data
                    JSONObject obj = new JSONObject(json);
                    JSONArray finalObj = obj.getJSONArray("data");
                    array_months = new ArrayList<String>();

                    for (int i = 0; i < finalObj.length(); i++) {

                        final String message = finalObj.getJSONObject(i)
                                .getString("message");

                        array_months.add(message);
                    }



                } catch (JSONException e) {
                    e.printStackTrace();
                }

        }

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

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

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

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

        // Create a simple array adapter (of type string) with the test values
        //ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, adapter);
        ListAdapter adapter = new ArrayAdapter<String>(MessagesActivity.this,
                android.R.layout.simple_list_item_1, array_months);
        //setListAdapter(listAdapter);
        return adapter;
    }
}

从我的主要活动中单击按钮

/**
         * Get Posts from Group
         * */
        btnGetPost.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // getGroupPosts();
                //getGPosts();

                startActivity(new Intent(FBAppActivity.this, MessagesActivity.class));
            }
        });

当我单击按钮时,我收到此错误

10-28 01:48:28.585: E/AndroidRuntime(1299): FATAL EXCEPTION: main
10-28 01:48:28.585: E/AndroidRuntime(1299): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.fbapp/org.example.fbapp.MessagesActivity}: java.lang.NullPointerException
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.app.ActivityThread.access$600(ActivityThread.java:156)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.os.Looper.loop(Looper.java:153)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.app.ActivityThread.main(ActivityThread.java:5297)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at java.lang.reflect.Method.invoke(Method.java:511)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at dalvik.system.NativeStart.main(Native Method)
10-28 01:48:28.585: E/AndroidRuntime(1299): Caused by: java.lang.NullPointerException
10-28 01:48:28.585: E/AndroidRuntime(1299):     at org.example.fbapp.MessagesActivity.createAdapter(MessagesActivity.java:74)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at org.example.fbapp.MessagesActivity.onCreate(MessagesActivity.java:62)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.app.Activity.performCreate(Activity.java:5122)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
10-28 01:48:28.585: E/AndroidRuntime(1299):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
10-28 01:48:28.585: E/AndroidRuntime(1299):     ... 11 more

任何指针为什么?

4

1 回答 1

2

array_months 在方法中异步加载,onComplete但您没有等待它设置适配器。array_months尚未填写,因此您将获得 NPE。将您的适配器创建方法放在方法下onComplete并在此之后填写列表。

于 2013-10-27T20:27:10.150 回答