gpt4 book ai didi

java - 在 onCreate 中使用 webview.loadUrl 时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 02:50:49 25 4
gpt4 key购买 nike

我有一个 webview,我希望它在应用程序启动后立即加载一个 url。

如果我在按下按钮时调用 loadURL(),它可以正常工作,但是当我尝试在 onCreate() 中加载 url 时,应用程序崩溃了。

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;
}
}

}

最佳答案

解决了!

我终于遇到了这个。

@Override
protected void onStart()
{
super.onStart();
myWebView = (WebView) findViewById(R.id.WebView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.google.com");
}

显然 fragment 没有立即加载

getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();

这需要一些时间来处理,所以在此之后执行的 onStart 完美地完成了工作。

关于java - 在 onCreate 中使用 webview.loadUrl 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24220675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com