我有一个显示webView的应用程序,每次在webView中加载网址时,我都会尝试显示一条自定义Toast消息,其中显示"请等待" ..所以我在此处填写此自定义Toast消息shouldOverrideUrlLoading(WebView视图,String url)方法..如果我启动应用程序并等待网址加载,该应用程序工作正常..并且加载网址时也会显示该消息..
当我启动应用程序然后突然退出它时,问题就出来了......无需等待加载的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>
答案 0 :(得分:0)
尝试像这样烤面包,看看。
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方法
中