Turbolinks适配器无法应用于MainActivity

时间:2016-10-26 09:33:46

标签: android turbolinks-5 turbolinks-android

我正在关注turbolinks-android文档,https://github.com/turbolinks/turbolinks-android,试图为使用turbolinks构建的Rails站点设置一个android包装器。遇到主线上的这一行.adapter(this)的问题。 下面我将发布我的MainActivity.java和堆栈跟踪。所有其他代码与我在此处发布的上一个问题相同(TurbolinksView not being found on Android)。任何帮助表示赞赏。谢谢:)

MainActivity.java:

package com.example.turbolinkswrapper;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.basecamp.turbolinks.TurbolinksSession;
import com.basecamp.turbolinks.TurbolinksView;


public class MainActivity extends AppCompatActivity {

    private TurbolinksView turbolinksView;

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

        // Assumes an instance variable is defined. Find the view you added to your
        // layout in step 1.

        turbolinksView = (TurbolinksView) findViewById(R.id.turbolinks_view);

        TurbolinksSession.getDefault(this)
                .activity(this)
                .adapter(this)
                .view(turbolinksView)
                .visit("https://basecamp.com");
    }
}

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.turbolinkswrapper, PID: 5540
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.turbolinkswrapper/com.example.turbolinkswrapper.MainActivity}: java.lang.ClassCastException: com.example.turbolinkswrapper.MainActivity cannot be cast to com.basecamp.turbolinks.TurbolinksAdapter
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: java.lang.ClassCastException: com.example.turbolinkswrapper.MainActivity cannot be cast to com.basecamp.turbolinks.TurbolinksAdapter
                      at com.example.turbolinkswrapper.MainActivity.onCreate(MainActivity.java:26)
                      at android.app.Activity.performCreate(Activity.java:6679)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Application terminated.

1 个答案:

答案 0 :(得分:2)

根据step two,您需要实现TurboLinksAdapter接口:

package com.example.turbolinkswrapper;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.basecamp.turbolinks.TurbolinksSession;
import com.basecamp.turbolinks.TurbolinksView;


public class MainActivity extends AppCompatActivity implements TurbolinksAdapter

    private TurbolinksView turbolinksView;

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

        // Assumes an instance variable is defined. Find the view you added to your
        // layout in step 1.

        turbolinksView = (TurbolinksView) findViewById(R.id.turbolinks_view);

        TurbolinksSession.getDefault(this)
                .activity(this)
                .adapter(this)
                .view(turbolinksView)
                .visit("https://basecamp.com");
    }

    @Override
    public void onPageFinished() {

    }

    @Override
    public void onReceivedError(int errorCode) {

    }

    @Override
    public void pageInvalidated() {

    }

    @Override
    public void requestFailedWithStatusCode(int statusCode) {

    }

    @Override
    public void visitCompleted() {

    }

    @Override
    public void visitProposedToLocationWithAction(String location, String action) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(INTENT_URL, location);
        this.startActivity(intent);
    }
}