Android 每当我试图通过按钮启动webview活动时,它都会显示应用程序已停止
我正在尝试创建一个应用程序,每当你按下一个按钮时,它就会实现webview活动。不幸的是,每当我按下它时,它都会给我一条信息,而不是去看它。一切似乎都是正确的,我将其通过调试器,它给了我错误“E/AndroidRuntime:FATAL EXCEPTION:main 进程:com.example.time\u应用程序,PID:7599 java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.time\u app/com.example.time\u app.Browser}:android.view.InflateException:二进制XML文件行#8:对类android.webkit.WebView进行膨胀时出错。它说问题出现在Browser.java文件的第42行,即“setContentView(R.layout.activity\u Browser);”。我检查了我所有的代码行,似乎都没有问题 Browser.javaAndroid 每当我试图通过按钮启动webview活动时,它都会显示应用程序已停止,android,android-intent,webview,Android,Android Intent,Webview,我正在尝试创建一个应用程序,每当你按下一个按钮时,它就会实现webview活动。不幸的是,每当我按下它时,它都会给我一条信息,而不是去看它。一切似乎都是正确的,我将其通过调试器,它给了我错误“E/AndroidRuntime:FATAL EXCEPTION:main 进程:com.example.time\u应用程序,PID:7599 java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.time\u app/com.
package com.example.time_app;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.Calendar;
import java.util.Date;
public class Browser extends AppCompatActivity {
//creating the webview variable
WebView wb;
//the end variable takes in the finshed load time(it is a long function because of the number length
long end;
//this is just a varibale to be used for the alert code
final Context context = this;
//this is the total number converted as a string because the alert function does not take in float
String numberAsString;
//the start variable takes in the start load time(it is a long function because of the number length
long start;
//The total variable takes in the substraction of the end and start and is a float variale do to its shorter length
float total;
Date end_Time;
Date start_Time;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
wb = (WebView) findViewById(R.id.webview);
wb.setWebViewClient(new WebViewClient() {
//this function records the load time
public void onPageStarted(WebView view, String url, Bitmap favicon) {
start = System.currentTimeMillis();
start_Time = Calendar.getInstance().getTime();
}
//This function will take in the finished load time and create an alert
@RequiresApi(api = Build.VERSION_CODES.O)
public void onPageFinished(WebView view, String url) {
end = System.currentTimeMillis();
total=end-start;
end_Time = Calendar.getInstance().getTime();
// Total is the difference in milliseconds.
// Dividing by 1000, you convert it to seconds
numberAsString = String.valueOf(total/1000);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Load Time");
// set dialog message
alertDialogBuilder
.setMessage("Elapsed Time:"+numberAsString+"\n"+"Start Time:"+start_Time+"\n"+"EndTime:"+end_Time+"\n")
.setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
//declaring and setting the web setting variable
WebSettings webSettings = wb.getSettings();
//setting up the javascript to allow thw browser to use javascript
webSettings.setJavaScriptEnabled(true);
//loading url
wb.loadUrl("https://www.aa.com");
}
}
activity_browser.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"
tools:context=".Browser">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout >
手动菜单.java
package com.example.time_app;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class Manual_menu extends AppCompatActivity {
public ListView lv;
public EditText nametxt;
public Button addbtn,updatebtn,deletebtn,submit;
public ArrayList<String> names=new ArrayList<String>();
public ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manual_menu);
lv=(ListView) findViewById(R.id.Listview1);
nametxt=(EditText) findViewById(R.id.nametxt);
addbtn=(Button) findViewById(R.id.addbtn);
updatebtn=(Button) findViewById(R.id.updatebtn);
deletebtn=(Button) findViewById(R.id.deletebtn);
submit=(Button) findViewById(R.id.Submit_btn);
//adapter
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,names);
lv.setAdapter(adapter);
//Set Selected Item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
nametxt.setText(names.get(i));
}
});
//add button event
addbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
add();
}
});
//update button event
updatebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
update();
}
});
//delete button event
deletebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
delete();
}
});
//clear button event
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivityDecision();
}
});
}
//add
private void add()
{
String name=nametxt.getText().toString();
if(!name.isEmpty() && name.length()>0)
{
//Add
adapter.add(name);
//Refresh
adapter.notifyDataSetChanged();
nametxt.setText("");
Toast.makeText(getApplicationContext(), "Added " +name,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "!!Nothing to Add " +name,Toast.LENGTH_SHORT).show();
}
}
//Update
private void update()
{
String name=nametxt.getText().toString();
int pos=lv.getCheckedItemPosition();
if(!name.isEmpty() && name.length()>0)
{
//Remove Item
adapter.remove(names.get(pos));
//insert
adapter.insert(name,pos);
//refresh
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Updated " +name,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "!!Nothing to Update " ,Toast.LENGTH_SHORT).show();
}
}
//delete
private void delete()
{
int pos=lv.getCheckedItemPosition();
if(pos > -1)
{
//remove
adapter.remove(names.get(pos));
//refresh
adapter.notifyDataSetChanged();
nametxt.setText("");
Toast.makeText(getApplicationContext(), "Deleted " ,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "!!Nothing to delete " ,Toast.LENGTH_SHORT).show();
}
}
//Submit
private void openActivityDecision()
{
Intent intent=new Intent(this, Browser.class);
startActivity(intent);
}
}
package com.example.time\u应用程序;
导入androidx.appcompat.app.appcompat活动;
导入java.util.ArrayList;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.Toast;
公共类手动菜单扩展了AppCompatActivity{
公共列表视图lv;
公共编辑文本名称TXT;
公共按钮addbtn、updatebtn、deletebtn、submit;
public ArrayList name=new ArrayList();
公共阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u manual\u菜单);
lv=(ListView)findViewById(R.id.Listview1);
nametxt=(EditText)findViewById(R.id.nametxt);
addbtn=(按钮)findviewbyd(R.id.addbtn);
updatebtn=(按钮)findViewById(R.id.updatebtn);
deletebtn=(按钮)findviewbyd(R.id.deletebtn);
提交=(按钮)findViewById(R.id.submit\u btn);
//适配器
adapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u single\u choice,name);
低压设置适配器(适配器);
//设置所选项目
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
nametxt.setText(names.get(i));
}
});
//添加按钮事件
addbtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
添加();
}
});
//更新按钮事件
updatebtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
更新();
}
});
//删除按钮事件
deletebtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
删除();
}
});
//清除按钮事件
submit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
openActivityDecision();
}
});
}
//加
私有void add()
{
String name=nametxt.getText().toString();
如果(!name.isEmpty()&&name.length()>0)
{
//加
adapter.add(名称);
//刷新
adapter.notifyDataSetChanged();
nametxt.setText(“”);
Toast.makeText(getApplicationContext(),“Added”+名称,Toast.LENGTH\u SHORT.show();
}否则{
Toast.makeText(getApplicationContext(),“!!无需添加“+名称,Toast.LENGTH_SHORT).show();
}
}
//更新
私有void更新()
{
String name=nametxt.getText().toString();
int pos=lv.getCheckedItemPosition();
如果(!name.isEmpty()&&name.length()>0)
{
//删除项目
adapter.remove(name.get(pos));
//插入
适配器。插入(名称、位置);
//刷新
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),“Updated”+名称,Toast.LENGTH\u SHORT.show();
}否则{
Toast.makeText(getApplicationContext(),“!!无需更新”,Toast.LENGTH\u SHORT.show();
}
}
//删除
私有void delete()
{
int pos=lv.getCheckedItemPosition();
如果(位置>-1)
{
//除去
adapter.remove(name.get(pos));
//刷新
adapter.notifyDataSetChanged();
nametxt.setText(“”);
Toast.makeText(getApplicationContext(),“Deleted”,Toast.LENGTH\u SHORT.show();
}否则{
Toast.makeText(getApplicationContext(),“!!无需删除”,Toast.LENGTH\u SHORT.show();
}
}
//提交
私有void openActivityDecision()
{
Intent Intent=新Intent(这个,Browser.class);
星触觉(意向);
}
}
activity_manual_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Manual_menu">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:text="Name :"
/>
<EditText
android:id="@+id/nametxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:ems="10"
/>
<ListView
android:id="@+id/Listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/addbtn"
android:choiceMode="singleChoice"
android:layout_marginTop="20dp">
</ListView>
<Button
android:id="@+id/addbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Listview1"
android:layout_below="@+id/nametxt"
android:layout_marginTop="19dp"
android:text="add" />
<Button
android:id="@+id/updatebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Listview1"
android:layout_toRightOf="@+id/addbtn"
android:layout_marginTop="19dp"
android:text="Update" />
<Button
android:id="@+id/Submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Listview1"
android:layout_alignParentRight="true"
android:layout_marginTop="19dp"
android:text="Submit" />
<Button
android:id="@+id/deletebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Listview1"
android:layout_toRightOf="@+id/updatebtn"
android:layout_marginTop="19dp"
android:text="Delete" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.time_app">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".File_menu"></activity>
<activity android:name=".Browser" />
<activity android:name=".file_load" />
<activity android:name=".Manual_menu" />
<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>
OK:崩溃发生在“Browser.onCreate()”中,但问题在于布局“activity\u Browser.xml”。建议:尝试将ID从“WebVIEW”改为“<代码> Android:ID=”+ ID/WebLeVIEW“< /Cordy>”。此外,请考虑发布更多的堆栈跟踪。请查看链接,并添加更多的堆栈回溯。这可能给出了具体说明为什么WebVIEW不会膨胀的线索。