Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从其他活动访问PrgressDialog_Android_Android Fragments_Android Activity_Progressdialog_Android Dialog - Fatal编程技术网

Android 从其他活动访问PrgressDialog

Android 从其他活动访问PrgressDialog,android,android-fragments,android-activity,progressdialog,android-dialog,Android,Android Fragments,Android Activity,Progressdialog,Android Dialog,我有一个MainActivity,在它的oncreate方法中加载一个片段,还有一个对话框来检查用户的输入。此片段在其onPageStarted方法中加载WebView和ProgressDialog。问题是,现在这两个对话框在应用程序启动时相互重叠,因此用户必须等待ProgressDialog(WebView)完成,然后才能访问InputDialog 我可以从我的MainActivity访问ProgressDialog吗?或者我可以在完成(取消)我的MainActivity的ProgressDi

我有一个MainActivity,在它的oncreate方法中加载一个片段,还有一个对话框来检查用户的输入。此片段在其onPageStarted方法中加载WebView和ProgressDialog。问题是,现在这两个对话框在应用程序启动时相互重叠,因此用户必须等待ProgressDialog(WebView)完成,然后才能访问InputDialog

我可以从我的MainActivity访问ProgressDialog吗?或者我可以在完成(取消)我的MainActivity的ProgressDialog后以任何方式显示输入对话框吗

创建时的主要活动:

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBar actionBar;

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment FirstFragment = new FirstFragment();
Fragment SecondFragment = new SecondFragment();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String firstrun = preferences.getString("firstrun", null);
    String helpread = preferences.getString("helpread", null);

    //check for users first time to open the app
    //opens the FirstRunActivity if so
    if (firstrun == null) {

        Intent FirstRunActivity = new Intent(this, FirstRunActivity.class);
        startActivity(FirstRunActivity);
    }

    //check for users first time to open the app
    //opens the HelpActivity if so
    if (helpread == null) {

        new MaterialDialog.Builder(this)
                .title("Hilfe Sektion lesen!")
                .content("Bitte lies die Hilfe Sektion der App um alle Informationen über die App zu erhalten und Fehler zu vermeiden.")
                .positiveText("OK")
                .callback(new MaterialDialog.ButtonCallback() {
                    @Override
                    public void onPositive(MaterialDialog dialog) {
                        Intent HelpActivity = new Intent(MainActivity.this, HelpActivity.class);
                        startActivity(HelpActivity);
                    }
                })
                .show();
    }
    //Replaces/adds the name and the grade of the users preferences to the header of the NavigationDrawer
    String name = preferences.getString("name_preference", "");
    String grade = preferences.getString("grade_preference", "");

    TextView header_username = (TextView) findViewById(R.id.header_username);
    TextView header_grade = (TextView) findViewById(R.id.header_grade);
    header_username.setText("Name: " + name);
    header_grade.setText("Klasse: " + grade);

    //initiate the Toolbar/Actionbar and adds the white menu Icon to it
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    actionBar = getSupportActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
    actionBar.setDisplayHomeAsUpEnabled(true);

    //initiates the NavigationDrawer
    drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    if (navigationView != null) {
        setupNavigationDrawerContent(navigationView);
    }

    setupNavigationDrawerContent(navigationView);

    //initiates the FirstFragment if there is no Saved Instance of the App
    if (savedInstanceState == null) {
        fragmentTransaction.replace(R.id.main_fragment, FirstFragment);
        fragmentTransaction.commit();
    }

}
FirstFragment oncreateview:

public class FirstFragment extends Fragment {

private ProgressBar ProgressBar;
private WebView webView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    //Initiate the Fragments Menu
    setHasOptionsMenu(true);

    View view = inflater.inflate(R.layout.first_fragment, container, false);

    //Initiates the FloatingActionButton and it's onClickListener
    FloatingActionButton button = (FloatingActionButton) view.findViewById(R.id.fab);
    button.setOnClickListener(new View.OnClickListener() {
        //Reloads the WebView if the User clicks on the FloatingActionButton
        @Override
        public void onClick(View v) {

            webView.reload();
        }
    });

    webView = (WebView) view.findViewById(R.id.webView);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(false);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);

    webView.setWebViewClient(new WebViewClient() {
        ProgressDialog prDialog;

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            new MaterialDialog.Builder(getActivity())
                    .title("Es ist etwas schief gelaufen :(")
                    .content("Es gibt ein Problem mit deiner Internetverbindung.\nBitte lade die Seite Neu oder besuche sie zu einem späteren Zeitpunkt nochmal.")
                    .positiveText("Ok")
                    .show();
        }

        //ProgressBar is Visible on a loading Page
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            prDialog = ProgressDialog.show(getActivity(), null, "Wird geladen, bitte warten...");

        }

        //PrgressBar is not visible on a finished Page and SnackBar with Hint
        @Override
        public void onPageFinished(WebView view, String url) {
            if (prDialog.isShowing()) {
                prDialog.dismiss();
            }
            Snackbar.make(view, "Hinweiß: 'oops...404 Error' bedeuted: \nEs sind aktuell keine Vertretungen für dich vorhanden.", Snackbar.LENGTH_LONG).show();
        }


    });

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String item = preferences.getString("grade_preference", "");

    webView.loadUrl("http://www.kantschule-falkensee.de/uploads/dmiadgspahw/vertretung/Druck_Kla_" + item + ".htm");


    return view;
}

通过在MainActivity中将ProgressDialog公开为静态,尝试使其全局可访问。
然后,只需执行MainActivity即可在MainActivity外部访问ProgressDialog。myProgressDialog是的,您可以定义如下界面:

public interface ProgressDialogDismissListener {
  void onProgressDialogDisMiss();
}

您可以初始化一个接口,并在方法“OnProgressDialogDismise”中执行您想要的操作,例如启动一个输入对话框。然后将该接口的引用传输到您的片段

对不起,这是一个非常糟糕的建议。持有对视图的引用将导致相当严重的内存泄漏。是的,您还应该在活动静态中执行每个视图。。。忘掉风景吧。。。让每一个变量都是静态的……那个么你们还有什么可以帮助我的建议吗?正如@spacitron所说的,这是一个非常糟糕的主意。。即使您尝试这样做,也会导致一些问题,如。。WindowManager$BadTokenException。。我不知道你想做什么,但是为什么不在显示片段之前等待用户处理主活动的对话框呢?如果你发布一些真实的代码也会有帮助。对不起,我确实知道你的问题所在。。你能发一些代码吗?这样我们就可以帮你……)