我有一个webview,我希望它在应用程序启动后立即加载网址。
如果我在按下按钮时调用loadURL()
它可以正常工作,但当我尝试在onCreate()
内加载网址时,该应用会崩溃。
WebView myWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void loadURL(View view) {
myWebView = (WebView) findViewById(R.id.WebView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.google.com");
}
当我使用它时应用程序崩溃:
WebView myWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
myWebView = (WebView) findViewById(R.id.WebView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.google.com");
}
编辑** 这就是我在我的控制台中获得的内容。
[2014-06-14 19:33:58 - ddms] Can't bind to local 8700 for debugger
[2014-06-14 19:34:11 - ddms] null
java.nio.BufferOverflowException
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:200)
at com.android.ddmlib.JdwpPacket.movePacket(JdwpPacket.java:235)
at com.android.ddmlib.Debugger.sendAndConsume(Debugger.java:347)
at com.android.ddmlib.Client.forwardPacketToDebugger(Client.java:665)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:344)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
[2014-06-14 19:34:12 - ddms] null
java.nio.BufferOverflowException
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:200)
at com.android.ddmlib.JdwpPacket.movePacket(JdwpPacket.java:235)
at com.android.ddmlib.Debugger.sendAndConsume(Debugger.java:347)
at com.android.ddmlib.Client.forwardPacketToDebugger(Client.java:665)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:344)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
编辑** 这是logcat输出
06-14 19:53:22.677: D/AndroidRuntime(8075): Shutting down VM
06-14 19:53:22.677: W/dalvikvm(8075): threadid=1: thread exiting with uncaught exception (group=0x41c11c80)
06-14 19:53:22.677: E/AndroidRuntime(8075): FATAL EXCEPTION: main
06-14 19:53:22.677: E/AndroidRuntime(8075): Process: com.myapp.myapp, PID: 8075
06-14 19:53:22.677: E/AndroidRuntime(8075): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.MainActivity}: java.lang.NullPointerException
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.app.ActivityThread.access$800(ActivityThread.java:145)
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.os.Looper.loop(Looper.java:136)
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.app.ActivityThread.main(ActivityThread.java:5088)
06-14 19:53:22.677: E/AndroidRuntime(8075): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 19:53:22.677: E/AndroidRuntime(8075): at java.lang.reflect.Method.invoke(Method.java:515)
06-14 19:53:22.677: E/AndroidRuntime(8075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
06-14 19:53:22.677: E/AndroidRuntime(8075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 19:53:22.677: E/AndroidRuntime(8075): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
06-14 19:53:22.677: E/AndroidRuntime(8075): at dalvik.system.NativeStart.main(Native Method)
06-14 19:53:22.677: E/AndroidRuntime(8075): Caused by: java.lang.NullPointerException
06-14 19:53:22.677: E/AndroidRuntime(8075): at com.myapp.myapp.MainActivity.onCreate(MainActivity.java:27)
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.app.Activity.performCreate(Activity.java:5310)
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 19:53:22.677: E/AndroidRuntime(8075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
06-14 19:53:22.677: E/AndroidRuntime(8075): ... 12 more
这里的任何用途都是完整的MainActivity.java
package com.myapp.myapp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
myWebView = (WebView) findViewById(R.id.WebView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.google.com");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
答案 0 :(得分:1)
解决了!
我终于遇到了这个。
@Override
protected void onStart()
{
super.onStart();
myWebView = (WebView) findViewById(R.id.WebView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.google.com");
}
显然片段没有立即加载
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
这需要一些时间来处理onStart
,这在此之后执行完美。
答案 1 :(得分:0)
这适用于我的
对于本地文件
WebView webView =(WebView) findViewById(R.id.webView1);
webView.loadUrl("file:///android_asset/programs.htm");
确保你有文件,在这种情况下,program.htm文件存储在android
上的 Assets 文件夹中对于网站
WebView webView =(WebView) findViewById(R.id.webView1);
webView.loadUrl("http://www.facebook.com");
在这种情况下,它会引导您到达目的地Facebok.com
希望这可以帮助你...