findViewByID()不起作用。我该怎么办?

时间:2014-12-28 13:25:01

标签: java android webview alertdialog findviewbyid

这是我的代码:

class MyDialogFragment extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Clear cache memory");
    builder.setMessage("Do you want to delete cache memory ?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
      {
            @Override
            public void onClick(DialogInterface p1, int p2)
            {
                //context.deleteDatabase("webview.db");
                //context.deleteDatabase("webviewCache.db");
                // TODO: Implement this method
                WebView browser = (WebView) findViewById(R.id.mainWebView1);
                browser.clearHistory();
                browser.clearFormData();
                browser.clearCache(true);
            }


        });

1 个答案:

答案 0 :(得分:1)

尝试使用getView()。这将返回片段的根视图,您可以调用它来调用findViewById()。

class MyDialogFragment extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Clear cache memory");
builder.setMessage("Do you want to delete cache memory ?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
  {
        @Override
        public void onClick(DialogInterface p1, int p2)
        {
            //context.deleteDatabase("webview.db");
            //context.deleteDatabase("webviewCache.db");
            // TODO: Implement this method
            WebView browser = (WebView) getView().findViewById(R.id.mainWebView1);
            browser.clearHistory();
            browser.clearFormData();
            browser.clearCache(true);
        }


    });