gpt4 book ai didi

android - 应用程序子类实现了所需的接口(interface),但系统拒绝

转载 作者:行者123 更新时间:2023-11-29 15:42:16 25 4
gpt4 key购买 nike

我正在尝试使用 Twilio 在他第一次注册时验证用户,作者 sending SMS到他输入的电话号码。

尽管我的 Application 子类实现了 LockProvider,但我在 LogCat 中收到以下错误:

Android Application object must implement LockProvider interface

这是我的代码:

public class RequestActivity extends Activity {

private static final String EMAIL = "EMAIL";
private static final String TOKEN = "TOKEN";
private static final String PICTURE_URL = "PICTURE_URL";

private OkHttpClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request);

final Intent intent = getIntent();
final String token = intent.getStringExtra(EMAIL);
String email = intent.getStringExtra(TOKEN);
String url = intent.getStringExtra(PICTURE_URL);

client = new OkHttpClient();

TextView emailLabel = (TextView) findViewById(R.id.username_label);
emailLabel.setText(email);
TextView tokenLabel = (TextView) findViewById(R.id.token_label);
tokenLabel.setText(token);
ImageView profileImageView = (ImageView) findViewById(R.id.profile_image);
Picasso.with(this).load(url).into(profileImageView);
final TextView nonSecureStatus = (TextView) findViewById(R.id.non_secured_status);
final TextView securedStatus = (TextView) findViewById(R.id.secured_status);

final HttpUrl pingUrl = HttpUrl.parse(getString(R.string.sample_api_base_url)).newBuilder()
.addPathSegment("ping")
.build();
final Request request = new Request.Builder()
.url(pingUrl)
.build();
pingWithRequest(request, new PingCallback() {
@Override
public void onResult(boolean success) {
nonSecureStatus.setText(success ? R.string.tick_symbol : R.string.cross_symbol);
nonSecureStatus.setTextColor(getResources().getColor(success ? R.color.request_success : R.color.request_fail));
}
});

Button callButton = (Button) findViewById(R.id.call_api_button);
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final HttpUrl pingUrl = HttpUrl.parse(getString(R.string.sample_api_base_url)).newBuilder()
.addPathSegment("secured")
.addPathSegment("ping")
.build();
final Request request = new Request.Builder()
.url(pingUrl)
.addHeader("Bearer " + token, "Authorization")
.build();
pingWithRequest(request, new PingCallback() {
@Override
public void onResult(boolean success) {
securedStatus.setText(success ? R.string.tick_symbol : R.string.cross_symbol);
securedStatus.setTextColor(getResources().getColor(success ? R.color.request_success : R.color.request_fail));
}
});
}
});
}

private void pingWithRequest(Request request, final PingCallback callback) {
final Handler handler = new Handler(getMainLooper());
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
callback.onResult(false);
}
});
}

@Override
public void onResponse(final Response response) throws IOException {
handler.post(new Runnable() {
@Override
public void run() {
callback.onResult(response.isSuccessful());
}
});
}
});
}


private interface PingCallback {
void onResult(boolean success);
}

public static Intent newIntent(Context context, String email, String token, String pictureUrl) {
final Intent intent = new Intent(context, RequestActivity.class);
intent.putExtra(EMAIL, email);
intent.putExtra(TOKEN, token);
intent.putExtra(PICTURE_URL, pictureUrl);
return intent;
}
}

我的应用类:

public class MyApplication extends Application implements LockProvider {

private Lock lock;

public void onCreate() {
super.onCreate();
lock = new Lock.Builder()
.loadFromApplication(this)
.closable(true)
.build();
}

@Override
public Lock getLock() {
return lock;
}
}

我的主要 Activity :

public class MainActivity extends Activity {

private LocalBroadcastManager broadcastManager;

private BroadcastReceiver authenticationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
UserProfile profile = intent.getParcelableExtra(Lock.AUTHENTICATION_ACTION_PROFILE_PARAMETER);
Token token = intent.getParcelableExtra(Lock.AUTHENTICATION_ACTION_TOKEN_PARAMETER);
startActivity(RequestActivity.newIntent(MainActivity.this, profile.getEmail(), token.getIdToken(), profile.getPictureURL()));
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button tryButton = (Button) findViewById(R.id.try_it_button);
tryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, LockSMSActivity.class));
}
});

broadcastManager = LocalBroadcastManager.getInstance(this);
broadcastManager.registerReceiver(authenticationReceiver, new IntentFilter(Lock.AUTHENTICATION_ACTION));
}

@Override
protected void onDestroy() {
super.onDestroy();
broadcastManager.unregisterReceiver(authenticationReceiver);
}
}

日志:

07-19 16:04:43.020 16817-16817/atoa.roomates E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{atoa.roomates/com.auth0.lock.sms.LockSMSActivity}: java.lang.IllegalStateException: Android Application object must implement LockProvider interface
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalStateException: Android Application object must implement LockProvider interface
at com.auth0.lock.Lock.getLock(Lock.java:314)
at com.auth0.lock.sms.LockSMSActivity.getLock(LockSMSActivity.java:184)
at com.auth0.lock.sms.LockSMSActivity.onCreate(LockSMSActivity.java:95)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
at android.app.ActivityThread.access$900(ActivityThread.java:172) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5832) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
07-19 16:04:43.225 16817-16817/atoa.roomates D/FirebaseCrashApiImpl: throwable java.lang.RuntimeException: Unable to start activity ComponentInfo{atoa.roomates/com.auth0.lock.sms.LockSMSActivity}: java.lang.IllegalStateException: Android Application object must implement LockProvider interface
07-19 16:04:43.305 16817-16817/atoa.roomates E/AndroidRuntime: FATAL EXCEPTION: main
Process: atoa.roomates, PID: 16817
java.lang.RuntimeException: Unable to start activity ComponentInfo{atoa.roomates/com.auth0.lock.sms.LockSMSActivity}: java.lang.IllegalStateException: Android Application object must implement LockProvider interface
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalStateException: Android Application object must implement LockProvider interface
at com.auth0.lock.Lock.getLock(Lock.java:314)
at com.auth0.lock.sms.LockSMSActivity.getLock(LockSMSActivity.java:184)
at com.auth0.lock.sms.LockSMSActivity.onCreate(LockSMSActivity.java:95)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
at android.app.ActivityThread.access$900(ActivityThread.java:172) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5832) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

最佳答案

您已经创建了自定义 Application类,但您还必须在 AndroidManifest.xml 中链接到它。

你的 <application>标签需要像这样(将 your_package_name 替换为您的 MyApplication 所在的包名称):

<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:name="<your_package_name>.MyApplication">

摘自 Android Developer Docs :

You can provide your own implementation by creating a subclass and specifying the fully-qualified name of this subclass as the "android:name" attribute in your AndroidManifest.xml's <application> tag

关于android - 应用程序子类实现了所需的接口(interface),但系统拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com