应用程序子类实现了所需的接口,但系统说不

时间:2016-07-19 13:20:33

标签: android twilio android-application-class

我正在尝试使用Twilio在用户首次注册时通过sending SMS验证他输入的电话号码。

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

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;
    }
}

我的主要活动:

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);
    }
}

logcat的:

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)

1 个答案:

答案 0 :(得分:4)

您已创建自定义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

  

您可以通过创建子类来提供自己的实现   将此子类的完全限定名称指定为   AndroidManifest.xml "android:name"中的<application>属性   标签