在Android上找不到TurbolinksView

时间:2016-10-25 15:56:40

标签: android turbolinks-5

我对Android比较陌生。我正在尝试为使用turbolinks构建的Rails站点实现Web包装器。我正在关注github上turbolinks-android repo的自述文件,但我遇到了这行turbolinksView = (TurbolinksView) findViewById(R.id.turbolinks_view);的问题 看起来Android无法在清单中找到turbolinks_view元素。我将在下面发布所有代码。任何帮助将非常感激。 :)

MainActivity.java

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 {

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


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

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

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.basecamp.turbolinks.TurbolinksView
    android:id="@+id/turbolinks_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.turbolinkswrapper">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.example.turbolinkswrapper"
        minSdkVersion 25
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.basecamp:turbolinks:1.0.4'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.0'
    testCompile 'junit:junit:4.12'
}

Error:(20, 9) error: cannot find symbol variable turbolinksView

1 个答案:

答案 0 :(得分:1)

您只需要在使用之前声明turbolinksView

更改您的

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

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

或以下可以让您更好地控制整个MainActivity

public class MainActivity extends AppCompatActivity {

    private TurbolinksView turbolinksView;

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


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

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