没有合适的方法findViewById

时间:2017-07-07 22:33:01

标签: java android

所以当我去运行应用程序时出现此错误,问题是我的webview代码适用于我以前的java文件,我所做的就是将false中的数字更改为ff1walkthrough h和ff2walkthrougff1

ff2

我的代码:

FinalFantasy2.Java

Error:(28, 36) error: no suitable method found for findViewById(WebView)
method Activity.<T>findViewById(int) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; WebView cannot be converted to int))
method AppCompatActivity.findViewById(int) is not applicable
(argument mismatch; WebView cannot be converted to int)
where T is a type-variable:
T extends View declared in method <T>findViewById(int)

HomeScreen.Java

package net.sunsetdevelopment.masteryourrpgs;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class FinalFantasy2 extends AppCompatActivity {
WebView ff2walkthrough;

/* Go Back Function */
@Override
public void onBackPressed() {
    if (ff2walkthrough.canGoBack()){
        ff2walkthrough.goBack();
    }else {
        super.onBackPressed();
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_final_fantasy2);
    ff2walkthrough = (WebView) findViewById(ff2walkthrough);

    /* Enable Javascript */
    ff2walkthrough.getSettings().setJavaScriptEnabled(true);
    ff2walkthrough.setFocusable(true);
    ff2walkthrough.setFocusableInTouchMode(true);

    /* Render Priority */
    ff2walkthrough.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    ff2walkthrough.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    ff2walkthrough.getSettings().setDomStorageEnabled(true);
    ff2walkthrough.getSettings().setDatabaseEnabled(true);
    ff2walkthrough.getSettings().setAppCacheEnabled(true);
    ff2walkthrough.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    /* URL */
    ff2walkthrough.loadUrl("link hidden for stackoverflow");
    ff2walkthrough.setWebViewClient(new WebViewClient());

    }




}

activity_final_fantasy2.xml

package net.sunsetdevelopment.masteryourrpgs;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class HomeScreen extends AppCompatActivity {

/* Declarations */
ImageButton ff1walkthrough;
ImageButton ff2walkthrough;

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

    /* Final Fantasy 1 Walkthrough */
    ff1walkthrough = (ImageButton) findViewById(R.id.ff1);

    ff1walkthrough.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intentLoadNewActivity = new Intent(HomeScreen.this, FinalFantasy1.class);
            startActivity(intentLoadNewActivity);
        }
    });

    /* Final Fantasy 2 Walkthrough */

    ff2walkthrough = (ImageButton) findViewById(R.id.ff2);

    ff2walkthrough.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intentLoadNewActivity = new Intent(HomeScreen.this, FinalFantasy2.class);
            startActivity(intentLoadNewActivity);
        }
    });

}
}

我仍然是Java编程/ Android应用程序开发的新手。我几乎没有阅读过一些书籍,只是简单地尝试通过实践和跟进YouTube教程来学习。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

将其更改为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_final_fantasy2);
    ff2walkthrough = (WebView) findViewById(R.id.ff2walkthrough); //HERE IS THE ERROR

    /* Enable Javascript */
    ff2walkthrough.getSettings().setJavaScriptEnabled(true);
    ff2walkthrough.setFocusable(true);
    ff2walkthrough.setFocusableInTouchMode(true);

    /* Render Priority */
    ff2walkthrough.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    ff2walkthrough.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    ff2walkthrough.getSettings().setDomStorageEnabled(true);
    ff2walkthrough.getSettings().setDatabaseEnabled(true);
    ff2walkthrough.getSettings().setAppCacheEnabled(true);
    ff2walkthrough.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    /* URL */
    ff2walkthrough.loadUrl("link hidden for stackoverflow");
    ff2walkthrough.setWebViewClient(new WebViewClient());

    }

答案 1 :(得分:0)

忘记添加对findViewById(R.id.ff2walkthrough); 的引用,如下所示:

ff2walkthrough

不是仅仅调用 /* Fan Output Up */ @IBAction func FanOutputUpBtn(_ sender: Any) { let name = "Fan Position\(fanCounter)" /* Setting the name of the fan position image including the variable fanCounter */ fanPositionImage.image = UIImage(named: "fanPosition \(fanCounter)") /* Setting the name of the fan position image including the variable fanCounter */ if fanCounter == 5 { /* Set maximum value */ } else { fanCounter += 1 /* Increase the fan counter position by 1 */ TestFanCounter.text = String(Int(self.fanCounter)) /* Display the counter image */ self.fanPositionImage.image = UIImage(named: name) /* Display image for fan position based on fan counter */ } ,而是进一步参考,请检查Docs以了解有关Android资源的更多信息。