在 shouldOverrideUrlLoading() 方法中膨胀自定义 toast 消息时出现 NullPointerException
NullPointerException while inflating custom toast message in shouldOverrideUrlLoading() method
我有一个显示 webView 的应用程序,每次在 webView 中加载 url 时,我都会尝试显示一条自定义 toast 消息,上面写着 "Please wait".. 所以我夸大了这个自定义 toast 消息在 shouldOverrideUrlLoading(WebView view, String url) 方法中。如果我启动应用程序并等待 url 加载,应用程序工作正常。加载 [=31 时也会出现消息=]..
当我启动应用程序然后突然退出时出现问题。没有等待 url 加载。应用程序崩溃,在 --
处出现空指针异常
LayoutInflater iflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
我无法找到防止此异常和应用程序崩溃的方法-
这是我的代码-
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "About to load:" + url);
view.loadUrl(url);
LayoutInflater nflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = nflater.inflate(R.layout.toast2,
(ViewGroup) getActivity().findViewById(R.id.tl1));
TextView textv = (TextView) layout.findViewById(R.id.text);
textv.setText(R.string.loading);
final Toast ltoast = new Toast(getActivity());
ltoast.setView(layout);
ltoast.show();
new CountDownTimer(9000, 1000)
{
public void onTick(long millisUntilFinished) {ltoast.show();}
public void onFinish() {ltoast.show();}
}.start();
return true;
}
这是我的 Logcat-
中的错误
07-30 15:26:25.429: E/AndroidRuntime(8701): FATAL EXCEPTION: main
07-30 15:26:25.429: E/AndroidRuntime(8701): java.lang.NullPointerException
07-30 15:26:25.429: E/AndroidRuntime(8701): at com.example.itslive.web1Activity$HelloWebViewClient.shouldOverrideUrlLoading(web1Activity.java:673)
07-30 15:26:25.429: E/AndroidRuntime(8701): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:384)
07-30 15:26:25.429: E/AndroidRuntime(8701): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 15:26:25.429: E/AndroidRuntime(8701): at android.os.Looper.loop(Looper.java:137)
07-30 15:26:25.429: E/AndroidRuntime(8701): at android.app.ActivityThread.main(ActivityThread.java:4921)
07-30 15:26:25.429: E/AndroidRuntime(8701): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 15:26:25.429: E/AndroidRuntime(8701): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 15:26:25.429: E/AndroidRuntime(8701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-30 15:26:25.429: E/AndroidRuntime(8701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-30 15:26:25.429: E/AndroidRuntime(8701): at dalvik.system.NativeStart.main(Native Method)
这是我的 toast2.xml 文件...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/blue"
android:paddingLeft="30dp"
android:paddingRight="30dp"
/>
</LinearLayout>
试试这样吐司看看
public void showCustomAlert()
{
Context context = getActivity();
// Create layout inflator object to inflate toast.xml file
LayoutInflater inflater = getActivity().getLayoutInflater();
// Call toast.xml file for toast layout
View toastRoot = inflater.inflate(R.layout.list_adapter, null);
Toast toast = new Toast(context);
// Set layout to toast
toast.setView(toastRoot);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
然后你必须做一件事。将 FragmentActivity 的对象创建为 onCreateView FragmentActivity fragActivity=getActivity();那么你必须调用 LayoutInflater inflater = fragActivity.getLayoutInflater();在你的 shouldOverrideUrlLoading 方法中
我有一个显示 webView 的应用程序,每次在 webView 中加载 url 时,我都会尝试显示一条自定义 toast 消息,上面写着 "Please wait".. 所以我夸大了这个自定义 toast 消息在 shouldOverrideUrlLoading(WebView view, String url) 方法中。如果我启动应用程序并等待 url 加载,应用程序工作正常。加载 [=31 时也会出现消息=]..
当我启动应用程序然后突然退出时出现问题。没有等待 url 加载。应用程序崩溃,在 --
处出现空指针异常LayoutInflater iflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
我无法找到防止此异常和应用程序崩溃的方法-
这是我的代码-
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "About to load:" + url);
view.loadUrl(url);
LayoutInflater nflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = nflater.inflate(R.layout.toast2,
(ViewGroup) getActivity().findViewById(R.id.tl1));
TextView textv = (TextView) layout.findViewById(R.id.text);
textv.setText(R.string.loading);
final Toast ltoast = new Toast(getActivity());
ltoast.setView(layout);
ltoast.show();
new CountDownTimer(9000, 1000)
{
public void onTick(long millisUntilFinished) {ltoast.show();}
public void onFinish() {ltoast.show();}
}.start();
return true;
}
这是我的 Logcat-
中的错误07-30 15:26:25.429: E/AndroidRuntime(8701): FATAL EXCEPTION: main
07-30 15:26:25.429: E/AndroidRuntime(8701): java.lang.NullPointerException
07-30 15:26:25.429: E/AndroidRuntime(8701): at com.example.itslive.web1Activity$HelloWebViewClient.shouldOverrideUrlLoading(web1Activity.java:673)
07-30 15:26:25.429: E/AndroidRuntime(8701): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:384)
07-30 15:26:25.429: E/AndroidRuntime(8701): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 15:26:25.429: E/AndroidRuntime(8701): at android.os.Looper.loop(Looper.java:137)
07-30 15:26:25.429: E/AndroidRuntime(8701): at android.app.ActivityThread.main(ActivityThread.java:4921)
07-30 15:26:25.429: E/AndroidRuntime(8701): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 15:26:25.429: E/AndroidRuntime(8701): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 15:26:25.429: E/AndroidRuntime(8701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-30 15:26:25.429: E/AndroidRuntime(8701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-30 15:26:25.429: E/AndroidRuntime(8701): at dalvik.system.NativeStart.main(Native Method)
这是我的 toast2.xml 文件...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/blue"
android:paddingLeft="30dp"
android:paddingRight="30dp"
/>
</LinearLayout>
试试这样吐司看看
public void showCustomAlert()
{
Context context = getActivity();
// Create layout inflator object to inflate toast.xml file
LayoutInflater inflater = getActivity().getLayoutInflater();
// Call toast.xml file for toast layout
View toastRoot = inflater.inflate(R.layout.list_adapter, null);
Toast toast = new Toast(context);
// Set layout to toast
toast.setView(toastRoot);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
然后你必须做一件事。将 FragmentActivity 的对象创建为 onCreateView FragmentActivity fragActivity=getActivity();那么你必须调用 LayoutInflater inflater = fragActivity.getLayoutInflater();在你的 shouldOverrideUrlLoading 方法中