1

尝试从 Facebook Graph API 接收数据,但我似乎得到了 NullPointerException。有人可以帮忙吗?这是我的带有 xml 和 logCat 输出的活动文件。

package com.example.fbtest;

// 省略所有导入语句。

    public class TestActivity extends Activity implements OnClickListener {

String APP_ID;
Facebook fb;
private SharedPreferences sp;
TextView tv;
ImageView login;
Button b;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    APP_ID = getString(R.string.APP_ID);
    fb = new Facebook(APP_ID);
    sp = getPreferences(MODE_PRIVATE);
    String access_token = sp.getString("access_token", null);
    long expires = sp.getLong("access_expires", 0);

    if (access_token != null) {
        fb.setAccessToken(access_token);
    }
    if (expires != 0) {
        fb.setAccessExpires(expires);
    }

    login = (ImageView) findViewById(R.id.login);
    login.setOnClickListener(this);
    b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_test, menu);
    return true;
}

private void post() {
    if (fb.isSessionValid()) {
        login.setImageResource(R.drawable.logout_button);
        JSONObject obj;
        try {
            obj = Util.parseJson(fb.request("ronakshah"));
            String id = obj.optString("id");
            String name = obj.optString("name");
            tv.setText("Welcome, " + name + id);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (FacebookError fe) {

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

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.login:

        if (fb.isSessionValid()) {
            // button close our session - log out of facebook
            try {
                fb.logout(getApplicationContext());
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            // login to facebook
            fb.authorize(TestActivity.this, new String[] { "email",
                    "read_stream" }, new DialogListener() {

                public void onFacebookError(FacebookError e) {
                    // TODO Auto-generated method stub
                    Toast.makeText(TestActivity.this, "fbError",
                            Toast.LENGTH_SHORT).show();
                }

                public void onError(DialogError e) {
                    // TODO Auto-generated method stub
                    Toast.makeText(TestActivity.this, "onError",
                            Toast.LENGTH_SHORT).show();
                }

                public void onComplete(Bundle values) {
                    // TODO Auto-generated method stub
                    Editor editor = sp.edit();
                    editor.putString("access_token", fb.getAccessToken());
                    editor.putLong("access_expires", fb.getAccessExpires());
                    editor.commit();
                }

                public void onCancel() {
                    // TODO Auto-generated method stub
                    Toast.makeText(TestActivity.this, "onCancel",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    case R.id.button1:
        post();
    }
}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="vertical"
>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/padding_medium"
    android:text="@string/hello_world"
    tools:context=".TestActivity" />

<ImageView
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/login_button"
    android:text="Button" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

日志猫:

07-18 14:10:42.636: I/dalvikvm(385): Jit: resizing JitTable from 512 to 1024
07-18 14:10:47.216: D/dalvikvm(385): GC_CONCURRENT freed 555K, 52% free 2998K/6151K, external 908K/1038K, paused 9ms+8ms
07-18 14:13:39.126: D/dalvikvm(385): GC_CONCURRENT freed 492K, 52% free 2988K/6151K, external 918K/1038K, paused 9ms+9ms
07-18 14:13:39.235: D/webviewglue(385): nativeDestroy view: 0x270d38
07-18 14:13:40.525: D/dalvikvm(385): GC_CONCURRENT freed 554K, 54% free 2867K/6151K, external 726K/1038K, paused 14ms+10ms
07-18 14:13:41.395: D/AndroidRuntime(385): Shutting down VM
07-18 14:13:41.395: W/dalvikvm(385): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-18 14:13:41.416: E/AndroidRuntime(385): FATAL EXCEPTION: main
07-18 14:13:41.416: E/AndroidRuntime(385): java.lang.NullPointerException
07-18 14:13:41.416: E/AndroidRuntime(385):  at com.example.fbtest.TestActivity.post(TestActivity.java:73)
07-18 14:13:41.416: E/AndroidRuntime(385):  at com.example.fbtest.TestActivity.onClick(TestActivity.java:136)
07-18 14:13:41.416: E/AndroidRuntime(385):  at android.view.View.performClick(View.java:2485)
07-18 14:13:41.416: E/AndroidRuntime(385):  at android.view.View$PerformClick.run(View.java:9080)
07-18 14:13:41.416: E/AndroidRuntime(385):  at android.os.Handler.handleCallback(Handler.java:587)
07-18 14:13:41.416: E/AndroidRuntime(385):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 14:13:41.416: E/AndroidRuntime(385):  at android.os.Looper.loop(Looper.java:123)
07-18 14:13:41.416: E/AndroidRuntime(385):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-18 14:13:41.416: E/AndroidRuntime(385):  at java.lang.reflect.Method.invokeNative(Native Method)
07-18 14:13:41.416: E/AndroidRuntime(385):  at java.lang.reflect.Method.invoke(Method.java:507)
07-18 14:13:41.416: E/AndroidRuntime(385):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-18 14:13:41.416: E/AndroidRuntime(385):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-18 14:13:41.416: E/AndroidRuntime(385):  at dalvik.system.NativeStart.main(Native Method)

提前致谢!

4

1 回答 1

1

你永远不会为你的tv变量分配任何东西。

假设你想TextView在你的 XML 文件中引用 ,你必须给它一个 id:

android:id="@+id/welcomeText"

然后将其分配给您的tv变量:

tv = (TextView) findViewById(R.id.welcomeText);
于 2012-07-18T09:32:18.243 回答