0

我刚刚学会了如何使用有关 facebook sdk 的全部内容,并将其放在我的代码中,但是当我在模拟器中运行应用程序时,应用程序启动,我登录然后启动主要活动,此时一切正常,但是当我启动第二个活动时,应用程序由于某种原因崩溃了,我希望你能帮助我。

主要活动:

package whao.whaoapps.kisscounter;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

import com.facebook.*;
import com.facebook.model.*;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Session.openActiveSession(this, true, new Session.StatusCallback(){

        public void call(Session session, SessionState state, Exception exception){
            if (session.isOpened()){

                Request.executeMeRequestAsync(session, new Request.GraphUserCallback(){

                    public void onCompleted(GraphUser user, Response response){
                        if (user!=null){
                            TextView welcome= (TextView)findViewById(R.id.welcome);
                            welcome.setText("Hello " + user.getName() + "!");
                        }
                    }
                });
        }
      }
    });
}

public void contar(View view) {
    Intent i = new Intent(this, Contar.class );
    startActivity(i);

}

public void creditos(View view) {
    Intent i = new Intent(this, Creditos.class );
    startActivity(i);

}


public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);

}

}

LogCat 错误:

07-19 05:57:06.251: E/AndroidRuntime(790): FATAL EXCEPTION: main
07-19 05:57:06.251: E/AndroidRuntime(790): java.lang.NullPointerException
07-19 05:57:06.251: E/AndroidRuntime(790):  at whao.whaoapps.kisscounter.MainActivity$1$1.onCompleted(MainActivity.java:31)
07-19 05:57:06.251: E/AndroidRuntime(790):  at com.facebook.Request$1.onCompleted(Request.java:264)
07-19 05:57:06.251: E/AndroidRuntime(790):  at com.facebook.Request$4.run(Request.java:1240)
07-19 05:57:06.251: E/AndroidRuntime(790):  at android.os.Handler.handleCallback(Handler.java:725)
07-19 05:57:06.251: E/AndroidRuntime(790):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 05:57:06.251: E/AndroidRuntime(790):  at android.os.Looper.loop(Looper.java:137)
07-19 05:57:06.251: E/AndroidRuntime(790):  at android.app.ActivityThread.main(ActivityThread.java:5039)
07-19 05:57:06.251: E/AndroidRuntime(790):  at java.lang.reflect.Method.invokeNative(Native Method)
07-19 05:57:06.251: E/AndroidRuntime(790):  at java.lang.reflect.Method.invoke(Method.java:511)
07-19 05:57:06.251: E/AndroidRuntime(790):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-19 05:57:06.251: E/AndroidRuntime(790):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-19 05:57:06.251: E/AndroidRuntime(790):  at dalvik.system.NativeStart.main(Native Method)

安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="whao.whaoapps.kisscounter"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="whao.whaoapps.kisscounter.MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="Contar"
          android:screenOrientation="portrait"
          android:theme="@android:style/Theme.NoTitleBar"></activity>

    <activity android:name="Creditos"
          android:screenOrientation="portrait"
          android:theme="@android:style/Theme.NoTitleBar"></activity>
    <activity android:name="Resultado"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar"></activity>
    <meta-data android:value="@string/app_id" android:name="com.facebook.sdk.ApplicationId"/>
    <activity android:name="com.facebook.LoginActivity"></activity>
</application>

</manifest>
4

1 回答 1

0

TextView welcome的大部分是空的。我认为你应该做两件事(第二件事只是一个建议): 1. 获取TextView welcomeinonCreate本身的引用,而不是在onCompleted. 2.在更新TextView的文本时,最好在UIThread(runOnUiThread)中进行

于 2013-07-19T06:10:21.133 回答