进度条在MainActivity中不起作用

时间:2017-01-17 08:55:11

标签: android progress-bar

我无法在显示的主要活动中实施进度条无法解析方法' findViewById'

我想知道为什么会这样显示,因为很多时候我遇到了这个问题,我是初学者,所以我要求开发人员帮助我。

这是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的屏幕截图

enter image description here

活动代码

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 个答案:

答案 0 :(得分:0)

  

你必须声明进度条全局并在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);
    }
    }

}

检查我的代码希望这会有所帮助。:)

答案 1 :(得分:0)

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;
}

答案 2 :(得分:0)

看看这个解决方案

search lvalues

答案 3 :(得分:0)

方法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;
    }
}