java.lang.NullPointerException android模拟器

时间:2013-07-19 06:04:09

标签: android facebook android-activity

我刚刚学会了如何使用关于facebook sdk的整个事情,我把它放在我的代码上但是当我在模拟器中运行应用程序时,应用程序启动,我登录然后启动主要活动,一切都没关系此时,但是当我开始第二个Activity时,应用程序因某些原因崩溃了,我希望你能帮助我。

MainAcivity:

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)

Android Manifest:

<?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>

1 个答案:

答案 0 :(得分:0)

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

中进行更新。