我正试图通过一个webview在我的片段中打开一个网页,但问题是我不能,而且我很遗憾地关闭了应用程序。
附上我正在使用的代码。
有任何建议或帮助吗?
感谢您的一切。
的.java
package com.example.dona1click;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Patrocinadores extends Fragment {
WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.nosotros, container, false);
String url = "10.1.1.8/dona1clickmobile/index.php";
// or
// WebView wv = (WebView)findViewById(R.id.my_webview);
mWebView.loadUrl(url);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
return x;
}
}
日志:
07-21 11:04:44.446: D/dalvikvm(1417): GC_FOR_ALLOC freed 45K, 3% free 4816K/4944K, paused 210ms, total 212ms
07-21 11:04:45.336: D/dalvikvm(1417): GC_FOR_ALLOC freed 6K, 3% free 5455K/5568K, paused 204ms, total 204ms
07-21 11:04:45.836: I/ActionBar(1417): null seleccionada.
07-21 11:04:46.146: D/dalvikvm(1417): GC_FOR_ALLOC freed 5K, 2% free 5525K/5628K, paused 202ms, total 207ms
07-21 11:04:46.146: I/dalvikvm-heap(1417): Grow heap (frag case) to 7.669MB for 2304016-byte allocation
07-21 11:04:46.376: D/dalvikvm(1417): GC_FOR_ALLOC freed <1K, 2% free 7774K/7880K, paused 227ms, total 227ms
07-21 11:04:49.916: I/Choreographer(1417): Skipped 85 frames! The application may be doing too much work on its main thread.
07-21 11:04:51.266: I/ActionBar(1417): null deseleccionada.
07-21 11:04:51.266: I/ActionBar(1417): null seleccionada.
07-21 11:04:51.556: D/dalvikvm(1417): GC_FOR_ALLOC freed 2504K, 32% free 5557K/8144K, paused 161ms, total 164ms
07-21 11:04:51.556: I/dalvikvm-heap(1417): Grow heap (frag case) to 8.000MB for 2618896-byte allocation
07-21 11:04:51.646: D/dalvikvm(1417): GC_FOR_ALLOC freed 1K, 25% free 8113K/10704K, paused 93ms, total 93ms
07-21 11:04:52.166: D/AndroidRuntime(1417): Shutting down VM
07-21 11:04:52.176: W/dalvikvm(1417): threadid=1: thread exiting with uncaught exception (group=0xb3a86ba8)
07-21 11:04:52.196: E/AndroidRuntime(1417): FATAL EXCEPTION: main
07-21 11:04:52.196: E/AndroidRuntime(1417): Process: com.example.dona1click, PID: 1417
07-21 11:04:52.196: E/AndroidRuntime(1417): java.lang.NullPointerException
07-21 11:04:52.196: E/AndroidRuntime(1417): at com.example.dona1click.Patrocinadores.onCreateView(Patrocinadores.java:26)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.app.Fragment.performCreateView(Fragment.java:1700)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.app.BackStackRecord.run(BackStackRecord.java:684)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.os.Handler.handleCallback(Handler.java:733)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.os.Handler.dispatchMessage(Handler.java:95)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.os.Looper.loop(Looper.java:136)
07-21 11:04:52.196: E/AndroidRuntime(1417): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-21 11:04:52.196: E/AndroidRuntime(1417): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 11:04:52.196: E/AndroidRuntime(1417): at java.lang.reflect.Method.invoke(Method.java:515)
07-21 11:04:52.196: E/AndroidRuntime(1417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-21 11:04:52.196: E/AndroidRuntime(1417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-21 11:04:52.196: E/AndroidRuntime(1417): at dalvik.system.NativeStart.main(Native Method)
07-21 11:04:54.686: I/Process(1417): Sending signal. PID: 1417 SIG: 9
07-21 11:04:57.496: D/dalvikvm(1443): GC_FOR_ALLOC freed 65K, 5% free 2809K/2956K, paused 72ms, total 75ms
07-21 11:04:57.496: I/dalvikvm-heap(1443): Grow heap (frag case) to 4.604MB for 1872016-byte allocation
07-21 11:04:57.566: D/dalvikvm(1443): GC_FOR_ALLOC freed <1K, 4% free 4636K/4788K, paused 58ms, total 58ms
07-21 11:04:58.296: D/gralloc_goldfish(1443): Emulator without GPU emulation detected.
答案 0 :(得分:0)
您的mWebView是否曾被分配给任何东西?您似乎通过调用findViewById(R.id.my_webview);
注释掉了分配webview的代码。但是,在这种情况下,变量名称是不同的。
您需要分配mWebView。此外,该布局文件中必须有一个您正在膨胀的webview。
尝试对尚未赋值的变量执行某些操作是抛出nullPointerException的最快方法。
WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.nosotros, container, false);
mWebView = (WebView) findViewById(R.id.my_webview);
String url = "10.1.1.8/dona1clickmobile/index.php";
// probably a good idea to check it's not null, to avoid these situations:
if(mWebView != null){
mWebView.loadUrl(url);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
return x;