我是新手。我的应用中有两项活动:登录和主要活动。我想在登录活动中输入我的应用密钥和应用秘密,但我的应用无效。它正在崩溃。我想在单击按钮时启动身份验证过程。
public class login_activity extends AppCompatActivity {
public String APP_kEY ;
public String APP_SECRET;
public String accessToken;
EditText app_key_view;
EditText App_secret_view;
Button dropbox;
public DropboxAPI<AndroidAuthSession> mDBApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences pref = getSharedPreferences("login",MODE_PRIVATE);
APP_kEY = pref.getString("appkey",null);
APP_SECRET = pref.getString("appsecret",null);
accessToken = pref.getString("access",null);
if (accessToken != null){
AppKeyPair appkeys = new AppKeyPair(APP_kEY,APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appkeys);
session.setOAuth2AccessToken(accessToken);
Intent i = new Intent(login_activity.this,MainActivity.class);
startActivity(i);
finish();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_activity);
app_key_view=(EditText)findViewById(R.id.app_key_text);
App_secret_view=(EditText)findViewById(R.id.app_secret_text);
dropbox = (Button)findViewById(R.id.link_dropbox);
//////////////////////////////////////////////////////////////
dropbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
APP_kEY = app_key_view.getText().toString();
APP_SECRET = App_secret_view.getText().toString();
AppKeyPair appKeys = new AppKeyPair(APP_kEY,APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
mDBApi = new DropboxAPI<>(session);
mDBApi.getSession().startOAuth2Authentication(login_activity.this);
}
});
}
@Override
protected void onResume () {
super.onResume();
if(mDBApi.getSession().authenticationSuccessful()){
try {
mDBApi.getSession().finishAuthentication();
accessToken = mDBApi.getSession().getOAuth2AccessToken();
SharedPreferences.Editor editor = getSharedPreferences("login",MODE_PRIVATE).edit();
editor.putString("appkey",APP_kEY);
editor.putString("appsecret",APP_SECRET);
editor.putString("access",accessToken);
editor.commit();
Intent i = new Intent(login_activity.this,MainActivity.class);
startActivity(i);
finish();
}
catch (IllegalStateException e){
Log.i("DbAuthLog", "Error authenticationg", e);
}
}
}
android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ayzaz.timescopev1.timescope" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".login_activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboard">
<intent-filter>
<!-- Change this to be db- followed by your app key -->
<data android:scheme="db-5400mvbvj5384st" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的
1633-1633 / com.example.ayzaz.timescopev1.timescope I / art:Late-capable -Xcheck:jni 09-11 20:59:32.437
1633-1633 / com.example.ayzaz.timescopev1.timescope D / AndroidRuntime: 关闭VM ---------崩溃开始09-11 20:59:32.438 1633-1633 / com.example.ayzaz.timescopev1.timescope E / AndroidRuntime: 致命异议:主要 处理:com.example.ayzaz.timescopev1.timescope,PID:1633 java.lang.RuntimeException:无法恢复活动{com.example.ayzaz.timescopev1.timescope / com.example.ayzaz.timescopev1.timescope.login_activity}: java.lang.NullPointerException:尝试调用虚方法 “com.dropbox.client2.session.Session 空对象上的com.dropbox.client2.DropboxAPI.getSession()' 参考 在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2986) 在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 引起:java.lang.NullPointerException:尝试调用虚方法'com.dropbox.client2.session.Session 空对象上的com.dropbox.client2.DropboxAPI.getSession()' 参考 at com.example.ayzaz.timescopev1.timescope.login_activity.onResume(login_activity.java:77) 在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257) 在android.app.Activity.performResume(Activity.java:6076) 在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975) 在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)09-11 20:59:43.201 1633-1633 / com.example.ayzaz.timescopev1.timescope I /处理:发送信号。 PID:1633 SIG:9