0

我正在关注 turbolinks-android 文档https://github.com/turbolinks/turbolinks-android.adapter(this) ,尝试为使用 turbolinks 5 构建的 Rails 站点设置 android 包装器。在 Main Activity 中遇到此行的问题. 下面我将发布我的 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.
4

1 回答 1

2

根据第二步,您需要实现 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);
    }
}
于 2016-10-26T09:41:44.447 回答