我剛剛學會了如何使用facebook sdk的全部內容,並將其放在我的代碼中,但是當我在模擬器中運行應用程序時,應用程序啓動,我登錄然後啓動主要活動,這一切都沒問題,但是當我開始第二個活動時,應用程序因某種原因崩潰,我希望你能幫助我。java.lang.NullPointerException android模擬器
的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清單:
<?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>
textview welcome或GraphUser用戶爲空。調試和檢查 –
只是一個想法:是否有可能您的TextView沒有設置id(在layout.xml中)? –