我想创建一个按钮,将webview重定向到某个页面。显示的错误是:'setOnclickListener之前从未使用'。我试过给 导入android.view.View.OnClickListener 但是没有用。
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import static sitesegundo.site.R.styleable.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//configuração do icone no title bar
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.icon);
getSupportActionBar().setDisplayUseLogoEnabled(true);
// configuração webview
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("file:///android_asset/www/index.html");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
// configuracao subtitulo titlebar
getSupportActionBar().setTitle(" Adote A4");
//getSupportActionBar().setSubtitle("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
Button button = (Button)findViewById(R.id.adocao);
button.setOnClickListener(new OnClickListener(Object arg0) {
public void onClick(View arg0) {
Intent viewIntent =
new Intent("android.intent.action.VIEW",
Uri.parse("http://www.stackoverflow.com/"));
startActivity(viewIntent);
}
});
}