Android - 应用程序崩溃没有任何明确的时间间隔

时间:2014-03-30 18:56:31

标签: android facebook sharedpreferences android-support-library android-facebook

我在我的应用中使用Facebook SDK,但是点击登录按钮后我的应用程序崩溃了。 但时间不固定。 有时它在我登录后崩溃,有时在我点击登录按钮的瞬间,有时在我填写登录详细信息之间。

也可以告诉我为什么共享偏好不起作用。

MainActivity.this

import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    Facebook fb;
    ImageView login;
    SharedPreferences sp;
    String APP_ID = "123456";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fb = new Facebook(APP_ID);
        sp = getPreferences(MODE_PRIVATE);
        String access_token = sp.getString("access_token", null);
        long expires = sp.getLong("expires", 0);
        if (access_token != null) {
            fb.setAccessToken(access_token);
        }
        if (expires != 0) {
            fb.setAccessExpires(expires);
        }
        login = (ImageView) findViewById(R.id.imageView1);
        login.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.imageView1) {
            fbLogin();
        }
    }

    private void fbLogin() {
        if (fb.isSessionValid()) {
            Intent i = new Intent(MainActivity.this, com.example.test.Register.class);
            startActivity(i);
        } else {
            fb.authorize(MainActivity.this, new String[] { "email" },
                    new DialogListener() {

                        @Override
                        public void onFacebookError(FacebookError e) {
                            // TODO Auto-generated method stub
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "fbError",Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onError(DialogError e) {
                            // TODO Auto-generated method stub
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "onError",Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        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();
                            Intent i = new Intent(MainActivity.this, com.example.test.Register.class);
                            startActivityForResult(i, RESULT_OK);
                        }

                        @Override
                        public void onCancel() {
                            // TODO Auto-generated method stub
                            Toast.makeText(MainActivity.this, "onCancel",Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        fb.authorizeCallback(requestCode, resultCode, data);
    }

}

所以请任何人帮我识别:

  1. 点击登录按钮后,为什么应用程序会随时崩溃。
  2. 为什么共享首选项不起作用。
  3. logcat的

    03-31 00:31:47.459: E/AndroidRuntime(5121): FATAL EXCEPTION: Timer-0
    03-31 00:31:47.459: E/AndroidRuntime(5121): java.lang.NoClassDefFoundError: android.support.v4.content.LocalBroadcastManager
    03-31 00:31:47.459: E/AndroidRuntime(5121):     at com.facebook.AppEventsLogger.flushAndWait(AppEventsLogger.java:760)
    03-31 00:31:47.459: E/AndroidRuntime(5121):     at com.facebook.AppEventsLogger.access$1(AppEventsLogger.java:732)
    03-31 00:31:47.459: E/AndroidRuntime(5121):     at com.facebook.AppEventsLogger$2.run(AppEventsLogger.java:605)
    03-31 00:31:47.459: E/AndroidRuntime(5121):     at java.util.Timer$TimerImpl.run(Timer.java:284)
    03-31 00:31:47.589: D/OpenGLRenderer(5121): Flushing caches (mode 0)
    03-31 00:31:50.319: I/Process(5121): Sending signal. PID: 5121 SIG: 9
    

1 个答案:

答案 0 :(得分:0)

关于共享偏好的问题:

看起来你在这里查找错误的键:

long expires = sp.getLong("expires", 0);

你应该把它改成:

long expires = sp.getLong("access_expires", 0);

因为稍后您确实将其保存在共享首选项中:

editor.putLong("access_expires",fb.getAccessExpires());
editor.commit();

注意:使用this related answer

解决了NoClassDefFoundError的其他问题