Индикатор выполнения не работает в MainActivity

i am not able to implement progress bar in main activity it is showing not resolve the method ‘findViewById’

и я хочу, почему он показывает, как это, потому что так много раз я застрял в этой проблеме, я новичок, поэтому я прошу надеюсь, что вы разработчик bro поможет мне.

вот код MainActivity

package com.fb.jaisonjoseph.facebookbasic;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import android.view.KeyEvent;
 /**
  * A simple {@link Fragment} subclass.
   */
  public class Home_Fragment extends Fragment {
   public ProgressBar bar;

public WebView mwebView;

public Home_Fragment() {

    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_home_, null);
    WebView view=(WebView) rootView.findViewById(R.id.webView);

    view.loadUrl("https://mbasic.facebook.com");
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyWebViewClient());
    //bar=(ProgressBar)rootView.findViewById(R.id.progressBar);

    return rootView;
    }

 private class MyWebViewClient extends WebViewClient {
    ProgressBar bar=(ProgressBar)findViewById(R.id.progressBar);

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    @Override
    public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
        bar.setVisibility(View.VISIBLE);

        super.onPageStarted(view, url, favicon);
    }
    @Override
    public void onPageFinished(WebView view, String url) {
        bar.setVisibility(View.GONE);

        super.onPageFinished(view, url);
    }
  }
}

вот скриншот MainActivity

Введите описание изображения здесь

код действия

package com.fb.jaisonjoseph.facebookbasic;

 import android.app.FragmentManager;
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.support.design.widget.NavigationView;
 import android.support.v4.view.GravityCompat;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.ViewGroup;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import android.widget.ProgressBar;

import java.security.PrivateKey;

  public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment




    View rootView = inflater.inflate(R.layout.fragment_home_, null);
    WebView view=(WebView) rootView.findViewById(R.id.webView);

    view.loadUrl("https://mbasic.facebook.com");
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyWebViewClient());

   bar=(ProgressBar)rootView.findViewById(R.id.progressBar);

    return rootView;
}



private class MyWebViewClient extends WebViewClient {


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    @Override
    public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
        bar.setVisibility(View.VISIBLE);

        super.onPageStarted(view, url, favicon);
    }
    @Override
    public void onPageFinished(WebView view, String url) {
        bar.setVisibility(View.GONE);

        super.onPageFinished(view, url);
    }
}






@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}


@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}











@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {

        Home_Fragment home_fragment=new Home_Fragment();
        android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relative_layour_fragment, home_fragment,home_fragment.getTag()).commit();

    } else if (id == R.id.nav_profile) {
       Profile_Fragment profile_fragment=new Profile_Fragment();
        android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relative_layour_fragment,profile_fragment,profile_fragment.getTag()).commit();
    } else if (id == R.id.nav_messages) {

    } else if (id == R.id.nav_notifications) {

    } else if (id == R.id.nav_chat) {

    } else if (id == R.id.nav_chat) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

4 ответа

  1. Вы должны объявить progress bar global и объявить в OncreateView, как это.

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.ProgressBar;
    
    import android.view.KeyEvent;
     /**
      * A simple {@link Fragment} subclass.
       */
      public class Home_Fragment extends Fragment {
       public ProgressBar bar;
    
    public WebView mwebView;
    ProgressBar bar;
    
    public Home_Fragment() {
    
        // Required empty public constructor
    }
    
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
    
    
    
    
        View rootView = inflater.inflate(R.layout.fragment_home_, null);
        bar=(ProgressBar)rootView.findViewById(R.id.progressBar);
        view.loadUrl("https://mbasic.facebook.com");
        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new MyWebViewClient());
        //bar=(ProgressBar)rootView.findViewById(R.id.progressBar);
    
        return rootView;
        }
    
    
    
     private class MyWebViewClient extends WebViewClient {
    
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    
        @Override
        public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
            bar.setVisibility(View.VISIBLE);
    
            super.onPageStarted(view, url, favicon);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            bar.setVisibility(View.GONE);
    
            super.onPageFinished(view, url);
        }
        }
    
    }
    

    Проверьте мой код надеюсь, что это поможет.:)

  2. ProgressBar bar;//declare as global
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home_, null);
    WebView view=(WebView) rootView.findViewById(R.id.webView);
    
    bar=(ProgressBar) rootView.findViewById(R.id.progressBar);
    
    view.loadUrl("https://mbasic.facebook.com");
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyWebViewClient()); 
    return rootView;
    }
    
  3. Взгляните на это решение

    public class Home_Fragment extends Fragment {
    public ProgressBar bar;
    
    public WebView mwebView;
    private View rootView;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
    
    
        rootView = inflater.inflate(R.layout.fragment_home_, null);
    
        bar=(ProgressBar)rootView.findViewById(R.id.progressBar);
    
        WebView view = (WebView) rootView.findViewById(R.id.webView);
    
        view.loadUrl("https://mbasic.facebook.com");
        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new MyWebViewClient());
    
        return rootView;
    }
    
    
    private class MyWebViewClient extends WebViewClient {
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    
        @Override
        public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
            bar.setVisibility(View.VISIBLE);
    
            super.onPageStarted(view, url, favicon);
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            bar.setVisibility(View.GONE);
    
            super.onPageFinished(view, url);
        }
    }
    }
    
  4. метод findViewByIdне принадлежит классуMyWebViewClient, к которому вы пытаетесь получить к нему доступ.

    чтобы устранить эту проблему, необходимо сохранить ссылку или ProgressBar в фрагменте, а затем получить к нему доступ из MyWebViewClient.
    измените код на:

    package com.fb.jaisonjoseph.facebookbasic;
    
    
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.ProgressBar;
    
    
    
    /**
     * A simple {@link Fragment} subclass.
     */
    public class Home_Fragment extends Fragment {
        public ProgressBar bar;
    
        public WebView mwebView;
    
        public Home_Fragment() {
    
            // Required empty public constructor
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
    
    
            View rootView = inflater.inflate(R.layout.fragment_home_, null);
            WebView view = (WebView) rootView.findViewById(R.id.webView);
            view.loadUrl("https://mbasic.facebook.com");
            view.getSettings().setJavaScriptEnabled(true);
            view.setWebViewClient(new MyWebViewClient());
            bar=(ProgressBar)rootView.findViewById(R.id.progressBar); //uncomment this
    
            return rootView;
        }
    
    
        private class MyWebViewClient extends WebViewClient {
    // ProgressBar bar=(ProgressBar)findViewById(R.id.progressBar); Not needed
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
    
            @Override
            public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
                bar.setVisibility(View.VISIBLE);
    
                super.onPageStarted(view, url, favicon);
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                bar.setVisibility(View.GONE);
    
                super.onPageFinished(view, url);
            }
        }
    
    
    }
    

    РЕДАКТИРОВАТЬ

    Также обновите код действия до:

    package com.fb.jaisonjoseph.facebookbasic;
    
    import android.app.FragmentManager;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.ViewGroup;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.ProgressBar;
    
    import java.security.PrivateKey;
    
    
    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {
    
    
    
    
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }
    
    
        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    
    
    
    
    
    
    
    
    
    
        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();
    
            if (id == R.id.nav_home) {
    
                Home_Fragment home_fragment=new Home_Fragment();
                android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
                manager.beginTransaction().replace(R.id.relative_layour_fragment, home_fragment,home_fragment.getTag()).commit();
    
            } else if (id == R.id.nav_profile) {
                Profile_Fragment profile_fragment=new Profile_Fragment();
                android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
                manager.beginTransaction().replace(R.id.relative_layour_fragment,profile_fragment,profile_fragment.getTag()).commit();
            } else if (id == R.id.nav_messages) {
    
            } else if (id == R.id.nav_notifications) {
    
            } else if (id == R.id.nav_chat) {
    
            } else if (id == R.id.nav_chat) {
    
            }
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    }