我遇到了一个问题,当我尝试启动应用程序时,遇到意外错误。我尝试解决它,但我的尝试都没有成功。
以下是代码:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.LoggingBehavior;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;
public class LoginActivity extends Activity {
private AlphaAnimation alphaDown;
private AlphaAnimation alphaUp;
private Button fbloginbtn;
private Session.StatusCallback statusCallback = new SessionStatusCallback();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
alphaDown = new AlphaAnimation(1.0f, 0.3f);
alphaUp = new AlphaAnimation(0.3f, 1.0f);
alphaDown.setDuration(100);
alphaUp.setDuration(100);
alphaDown.setFillAfter(true);
alphaUp.setFillAfter(true);
fbloginbtn = (Button)findViewById(R.id.fbloginbtn);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
// Add Session Callback Here
Session.getActiveSession().addCallback(statusCallback);
Session session = Session.getActiveSession();
if(session == null) {
if(savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if(session== null) {
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
// Get Avtive Session again in case of session is null
Session session1 = Session.getActiveSession();
}
}
@Override
public void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
public void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
private void updateView() {
final Session session = Session.getActiveSession();
fbloginbtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(session.isOpened()){
Toast.makeText(getApplicationContext(), "loginfb...", Toast.LENGTH_LONG).show();
fblogin();
}
else{
Toast.makeText(getApplicationContext(), "onClickLogin...", Toast.LENGTH_LONG).show();
onClickLogin();
}
}
});
}
public void fblogin() {
Intent intent = new Intent(LoginActivity.this, MoodActivity.class);
startActivity(intent);
}
private void onClickLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
}
Intent intent = new Intent(LoginActivity.this, MoodActivity.class);
startActivity(intent);
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
updateView();
}
}
}
以下是从logcat
显示的消息07-18 15:46:12.406: D/dalvikvm(12236): GC_FOR_ALLOC freed 54K, 6% free 2870K/3032K, paused 4ms, total 4ms
07-18 15:46:12.426: I/dalvikvm-heap(12236): Grow heap (frag case) to 25.335MB for 23520012-byte allocation
07-18 15:46:12.434: D/dalvikvm(12236): GC_FOR_ALLOC freed <1K, 1% free 25839K/26004K, paused 7ms, total 7ms
07-18 15:46:12.570: D/dalvikvm(12236): GC_FOR_ALLOC freed 1K, 1% free 27719K/27880K, paused 2ms, total 3ms
07-18 15:46:12.586: D/dalvikvm(12236): GC_FOR_ALLOC freed 2K, 1% free 29051K/29212K, paused 3ms, total 3ms
07-18 15:46:12.598: D/AndroidRuntime(12236): Shutting down VM
07-18 15:46:12.598: W/dalvikvm(12236): threadid=1: thread exiting with uncaught exception (group=0xa4cf1b20)
07-18 15:46:12.598: E/AndroidRuntime(12236): FATAL EXCEPTION: main
07-18 15:46:12.598: E/AndroidRuntime(12236): Process: com.dooba_demo_version, PID: 12236
07-18 15:46:12.598: E/AndroidRuntime(12236): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba_demo_version/com.dooba_demo_version.LoginActivity}: java.lang.NullPointerException
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.os.Looper.loop(Looper.java:136)
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-18 15:46:12.598: E/AndroidRuntime(12236): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 15:46:12.598: E/AndroidRuntime(12236): at java.lang.reflect.Method.invoke(Method.java:515)
07-18 15:46:12.598: E/AndroidRuntime(12236): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-18 15:46:12.598: E/AndroidRuntime(12236): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-18 15:46:12.598: E/AndroidRuntime(12236): at dalvik.system.NativeStart.main(Native Method)
07-18 15:46:12.598: E/AndroidRuntime(12236): Caused by: java.lang.NullPointerException
07-18 15:46:12.598: E/AndroidRuntime(12236): at com.dooba_demo_version.LoginActivity.onCreate(LoginActivity.java:51)
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.app.Activity.performCreate(Activity.java:5231)
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-18 15:46:12.598: E/AndroidRuntime(12236): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-18 15:46:12.598: E/AndroidRuntime(12236): ... 11 more
以下是我的Android清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dooba_demo_version"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="11" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/d21"
android:label="@string/app_name" >
<activity
android:name="com.dooba_demo_version.LoginActivity"
android:screenOrientation="portrait"
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="com.facebook.LoginActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<activity
android:name="com.dooba_demo_version.MoodActivity"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
</application>
</manifest>
更新处理NullPointerException
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
alphaDown = new AlphaAnimation(1.0f, 0.3f);
alphaUp = new AlphaAnimation(0.3f, 1.0f);
alphaDown.setDuration(100);
alphaUp.setDuration(100);
alphaDown.setFillAfter(true);
alphaUp.setFillAfter(true);
fbloginbtn = (Button)findViewById(R.id.fbloginbtn);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
// Add Session Callback Here
Session.getActiveSession().addCallback(statusCallback);
Session session = Session.getActiveSession();
if(session == null) {
if(savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if(session== null) {
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
// Get Avtive Session again in case of session is null
Session session1 = Session.getActiveSession();
}
else {
Session.getActiveSession().addCallback(statusCallback);
}
}
提前致谢
答案 0 :(得分:0)
此声明符合NullPointerException
Session.getActiveSession().addCallback(statusCallback);
要解决此问题,您需要在尝试调用addCallback
方法之前确保会话不为空,因此您需要将else
添加到if(session == null)
并将最后一个语句移到内部其他如下
// Get Avtive Session again in case of session is null
Session session1 = Session.getActiveSession();
} else {
Session.getActiveSession().addCallback(statusCallback);
}