谁能帮助我的第一个 WebView 我被这个问题困住了?不幸的是 <app name> 已停止
Posted
技术标签:
【中文标题】谁能帮助我的第一个 WebView 我被这个问题困住了?不幸的是 <app name> 已停止【英文标题】:Can anyone help my first WebView i am stucked witht this issue ? Unfortunately <app name> has stopped 【发布时间】:2016-06-18 06:44:45 【问题描述】:1-content_main.xml
<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_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="mx.newmx.MainActivity"
tools:showIn="@layout/activity_main">
<WebView
android:layout_
android:layout_
android:id="@+id/activity_main_webview" />
</RelativeLayout>
2-manifest.xml 文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mx.newmx">
<application
android:allowBackup="true"
android:largeHeap="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">
<activity
android:name="mx.newmx.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
3-MainActivity.java文件代码
package mx.newmx;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity
private WebView myWebView ;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);
myWebView = (WebView) myWebView.findViewById(R.id.activity_main_webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setjavascriptEnabled(true);
myWebView.loadUrl("http://www.moneyx.pk/");
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
4-我的日志请检查这个
03-04 14:48:25.448 7418-7418/mx.newmx W/AwContents: onDetachedFromWindow called when already detached. Ignoring
03-04 14:48:25.468 7418-7418/mx.newmx D/AndroidRuntime: Shutting down VM
03-04 14:48:25.469 7418-7418/mx.newmx E/AndroidRuntime: FATAL EXCEPTION: main
Process: mx.newmx, PID: 7418
java.lang.RuntimeException: Unable to start activity ComponentInfomx.newmx/mx.newmx.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.webkit.WebView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.webkit.WebView.findViewById(int)' on a null object reference
at mx.newmx.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
这就是我创建我的第一个 WebView 的方式,但不幸的是应用程序名称已停止,这会产生错误,请任何人检查代码的问题所在,以便我可以继续
这就是我创建我的第一个 WebView 的方式,但不幸的是应用程序名称已停止,这会产生错误,请任何人检查代码的问题所在,以便我可以继续
【问题讨论】:
检查 *** 上的java.lang.NullPointerException:
是什么。你得到了这个例外。
你能帮忙解决什么问题吗?
myWebView = (WebView) findViewById(R.id.activity_main_webview);
【参考方案1】:
myWebView = (WebView) myWebView.findViewById(R.id.activity_main_webview);
删除 myWebView 只使用这样的 findViewById
myWebView = (WebView) findViewById(R.id.activity_main_webview);
【讨论】:
【参考方案2】:您正在膨胀不同的布局。请检查
setContentView(R.layout.activity_main);
【讨论】:
你能解释一下是什么吗? 我认为你在不包含 webview 的活动中夸大了不同的布局..请检查 还是一样,我改了这个也没有运气以上是关于谁能帮助我的第一个 WebView 我被这个问题困住了?不幸的是 <app name> 已停止的主要内容,如果未能解决你的问题,请参考以下文章