html页面无法打开。我需要在web视图中打开html页面,点击另一个活动中的按钮。粘贴两个java文件:
prerequisites.java:
public class Prerequisites extends AppCompatActivity implements View.OnClickListener{
WebView wv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prerequisites);
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.bowls)
{
Intent intent = new Intent(this, bowls.class);
Button btbowls=(Button)findViewById(R.id.bowls);
startActivity(intent);
finish();
}
}
}
bowls.java:
public class bowls extends AppCompatActivity {
WebView wv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bowls);
wv = (WebView) findViewById(R.id.bowlswebview);
wv.loadUrl("file:///android_asset/bowls.html");
}
}
答案 0 :(得分:1)
@Elackya
如果您在activity_prerequisites中添加Web视图,则以下代码将起作用。
public class MainActivity extends AppCompatActivity {
public WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.button2);
final String webLink = getIntent().getStringExtra("weblink");
wv = (WebView) findViewById(R.id.webView);
wv.loadUrl(webLink);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.setAction("weblink");
intent.putExtra("weblink", "file:///android_asset/hello.html");
startActivity(intent);
}
});
}
}
如果您在另一个活动中添加您的网络视图,那么第二个活动的源代码如下所示
public class bowls extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
WebView ww = findViewById(R.id.webView2);
Intent web = getIntent();
String url = web.getStringExtra("weblink");
ww.loadUrl(url);
}
}
您的主要活动课程应该是这样的 公共类MainActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),bowls.class);
intent.setAction("weblink");
intent.putExtra("weblink", "file:///android_asset/hello.html");
startActivity(intent);
}
});
}
}
AndroidManifest.xml应包含活动碗
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".bowls"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme.NoActionBar">
</activity>
答案 1 :(得分:0)
它......它不是如何工作的,putExtra用于将信息传递给另一个活动,而不是做你想做的事情,因为你的意图毫无意义而崩溃了和intent用作发送到另一个活动的包,也没有意图获取
无论如何,这里是固定代码
public class Prerequisites extends AppCompatActivity implements View.OnClickListener{
WebView wv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prerequisites);
wv = (WebView) findViewById(R.id.bowlswebview);
}
@Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.bowls:
wv.loadUrl("file:///android_asset/bowls.html");
break;
default:
break;
}
}
}