我有一个拥有此代码的NullPointerException。我想问题是getView(),但我不知道另一种方法。
public class Seccion1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.seccion3, container, false);
return rootView;
}
@Override
public void onResume() {
super.onResume();
WebView mWebView;
mWebView = (WebView) getView().findViewById(R.id.lectura_webView);
//Archivo de ejemplo. Cambiar por el adecuado.
--> THIS IS THE LINE THAT GIVES THE ERROR mWebView.loadUrl("file:///android_asset/productividad/ejemplo1.html");
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.setLongClickable(true);
mWebView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
}
}
The log:
java.lang.RuntimeException: Unable to resume activity {com.ejemplo/ejemplo.ejemplo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.ejemplo.Seccion1.onResume(Seccion1.java:33)
at android.app.Fragment.performResume(Fragment.java:1743)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:924)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1863)
at android.app.Activity.performResume(Activity.java:5320)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
谢谢!
P.D:我正在添加此文本,因为它说我的帖子主要是代码。我认为这个问题是明确而直接的,我提供了正确识别问题所需的数据。再次感谢你!
修改1
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ejemplo/com.ejemplo1.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.ejemplo1.Seccion1.onCreateView(Seccion1.java:29)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5240)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
编辑2:
代码已更新,返回上一级日志:
public class Seccion1 extends Fragment {
WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.seccion3, container, false);
mWebView = (WebView) rootView.findViewById(R.id.lectura_webView);
//Archivo de ejemplo. Cambiar por el adecuado.
mWebView.loadUrl("file:///android_asset/productividad/ejemplo1.html");
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.setLongClickable(true);
mWebView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
return rootView;
}
答案 0 :(得分:1)
将WebView mWebView;
声明为类变量,并在onCreateView
中初始化为
mWebView = (WebView) rootView.findViewById(R.id.lectura_webView);
并从onResume
WebView mWebView;
mWebView = (WebView) getView().findViewById(R.id.lectura_webView);
答案 1 :(得分:0)
public class Seccion1 extends Fragment
{
WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.seccion3, container, false);
mWebView = (WebView)rootView.findViewById(R.id.lectura_webView);
//Archivo de ejemplo. Cambiar por el adecuado.
--> THIS IS THE LINE THAT GIVES THE ERROR
mWebView.loadUrl("file:///android_asset/productividad/ejemplo1.html");
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.setLongClickable(true);
mWebView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
return rootView;
}
}
答案 2 :(得分:0)
从onResume
:
mWebView = (WebView) getView().findViewById(R.id.lectura_webView);
并在onCreateView
中添加此代码:
mWebView = (WebView) rootView.findViewById(R.id.lectura_webView);
您需要执行此操作,因为mWebView
位于夸大的布局中(rootView
)。