如何在标签片段

时间:2016-01-19 01:55:06

标签: android tabs

目前我正在做一个Android应用程序,需要连接并使用tab来显示不同类别的产品。在活动中,我能够运行下面的代码以获得额外的代码,但无法在标签片段中运行。在选项卡片段中,showDialogBox,getExtra和isNetworkConnected()的函数类无法运行。我使用http://saulmm.github.io/mastering-coordinator/中显示的AppBarLayout示例和一些附加代码。 TQ

try { getExtra(savedInstanceState);
        if (!isNetworkConnected()) {
            if(language == 0)
                showDialogBox("Dashboard","No internet connection, please turn on your network connection");
            else if(language == 1)
                showDialogBox("主页","无法上网, 请打开网路.");
            else
                showDialogBox("Papan Pemuka","Tiada internet, sila buka sambungan internet anda");
            this.finish();
            return;
        }
        //some code here
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                   //some code here
                } catch (Exception ex) {

                }
            }
        }).start();

    } catch (Exception ex) {}

private void showDialogBox(String title, String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(message).setTitle(title);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        // @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

private void getExtra(Bundle savedInstanceState) {
    // get params frm previous page/////
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            custID = extras.getString("CUSID");
        }
    }
}
 private boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni == null) {
        Toast.makeText(getApplicationContext(), "No Network Connection.", Toast.LENGTH_LONG).show();
        return false;
    } else
        return true;
}

1 个答案:

答案 0 :(得分:0)

您可以通过getActivity()引用从片段中获取活动。 getActivity().getIntent().getExtras()

也是如此