在我的android应用程序中,我有一个片段活动,它有一个progressbar。当任何片段调用接口时,此进度条设置为可见。因此,fragment类的代码如下:

public class MainScreen extends FragmentActivity {

    public interface OnConnectingToInternet {
        public void showProgressbar(boolean flag);
    }

    // rest of codes
    .
    .
    .

    // Implementing Interface
    public void showProgressbar(boolean flag) {
        if(flag){
            myProgressbar.showProgressBar();
        } else {
            myProgressbar.hideProgressBar();
        }
    }

}

每个片段都应该连接到Internet并获取数据,但是在这之前它们应该调用接口。我为一个类编写了以下代码(其他片段的代码如下)。
public class TopRecipesFragment extends Fragment {

    private OnConnectingToInternet onConnectingToInternet;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // onConnectingToInternet = (OnConnectingToInternet) activity;

        Log.i(TAG, "Fragment attached to activity.");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        New MyAsyncTask.execute();
    }

    public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {

        @Override
            protected void onPreExecute() {
            Log.i(TAG, "myAsyncTask is about to start...");

            onConnectingToInternet.showProgressbar(true);
            }

        @Override
            protected Boolean doInBackground(Void... params) {
            ...
            }

        @Override
            protected void onPostExecute(Boolean result) {
            Log.i(TAG, "myAsyncTask is about to start...");

            onConnectingToInternet.showProgressbar(false);
            }

}

问题是我不知道如何实例化这个接口。如果我不放置onConnectingToInternet = (OnConnectingToInternet) activity;,那么在运行应用程序之后,它将在NullPointerException中崩溃。
但是,如果我这样实例化,那么应用程序会因为转换问题而崩溃。我也不能以正常的方式实例化它,比如onConnectingToInternet = new MainScreen();
解决办法是什么?任何建议都将不胜感激。谢谢

最佳答案

我会写信告诉你,你正在尝试以下方法:
onconnectingtointernet.java:

public interface OnConnectingToInternet {
        public void showProgressbar(boolean flag);
    }

主屏幕.java:
public class MainScreen extends FragmentActivity implements OnConnectingToInternet {


// rest of codes
.
.
.

// Implementing Interface
@Override
public void showProgressbar(boolean flag) {
    if(flag){
        myProgressbar.showProgressBar();
    } else {
        myProgressbar.hideProgressBar();
    }
}
}

topRecipesFragment.java:
public class TopRecipesFragment extends Fragment {

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.i(TAG, "Fragment attached to activity.");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        new MyAsyncTask((OnConnectingToInternet)getActivity()).execute();
    }

    // Make class static to avoid memory leaks
    public static class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {

        // Keep WeakReference to the interface to avoid memory leaks
        private final WeakReference<OnConnectingToInternet> connectionRef;

        MyAsyncTask(OnConnectingToInternet connection) {
            connectionRef = new WeakReference<OnConnectingToInternet>(connection);
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "myAsyncTask is about to start...");

            OnConnectingToInternet connection = connectionRef.get();

            if (null != connection) {
               connection.showProgressbar(true);
            }
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            ...
        }

        @Override
        protected void onPostExecute(Boolean result) {
            Log.i(TAG, "myAsyncTask is about to start...");

            OnConnectingToInternet connection = connectionRef.get();

            if (null != connection) {
               connection.showProgressbar(false);
            }
       }
}

关于android - 如何在 fragment 中实例化接口(interface)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11623468/

10-14 21:07