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" />

LogCat:

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変数に何も割り当てていません。

TextViewXML ファイルでを参照する場合、ID を指定する必要があります。

android:id="@+id/welcomeText"

次に、それをtv変数に割り当てます。

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