Just started programming and I'm coding my first app. I've been wrecking my brain over this for long enough. I picked this up from tutorial where a listener is implemented in a ListFragment and the event listener is in a FragmentActivity that hosts the ListFragment. Here is how it is in the example (showing only the relevant part):
刚开始编程,我正在编写我的第一个应用程序。我一直在破坏我的大脑足够长的时间。我从教程中选择了这个,其中一个监听器在ListFragment中实现,而事件监听器位于一个托管ListFragment的FragmentActivity中。以下是示例中的内容(仅显示相关部分):
MyList class:
public class MyList extends ListFragment{
OnSomeItemSelectedListener mCallback;
public interface OnSomeItemSelectedListener {
public void onSomeItemSelectedListener(int position, long id);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mCallback = (OnSomeItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnSomeItemSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mCallback.onSomeItemSelectedListener(position, id);
}
}
MainActivity class:
public class MainActivity extends FragmentActivity implements
MyList.OnSomeItemSelectedListener{
@Override
public void onSomeItemSelectedListener(int position, long id) {
//some stuff done here when clicked on a item from the listfragment
}
}
The above works flawlessly.
以上工作完美无瑕。
I'm trying to implement this in an application class to notify the event listener (a FragmentActivity) when an AsyncTask has finished. This is what I did in in the application class:
我正在尝试在应用程序类中实现它,以在AsyncTask完成时通知事件侦听器(FragmentActivity)。这就是我在应用程序类中所做的:
MyApplication class:
public class MyApplication extends Application {
OnFetchCompleteListener mCallback;
public interface OnFetchCompleteListener {
public void onFetchCompleteListener();
}
public synchronized void fetchUpdates() {
String[] mURLs = getURLs();
new DownloadDataAsyncTask().execute(mURLs[0], mURLs[1]);
}
private class DownloadDataAsyncTask extends
AsyncTask<String, Void, JSONObject[]> {
@Override
protected JSONObject[] doInBackground(String... params) {
//Some stuff
return mJSON;
}
@Override
protected void onPostExecute(JSONObject[] result) {
// Informing listeners on fetchData complete
mCallback.onFetchCompleteListener();
}
}
}
How do I instantiate the mCallback
in the application class? Without this,
如何在应用程序类中实例化mCallback?没有这个,
mCallback = (OnSomeItemSelectedListener) activity;
I get a null pointer exception from the application class.
我从应用程序类中得到一个空指针异常。
Many thanks
1 个解决方案
#1
2
I think you can instantiate mCallback from the Activity instead of from the Application.
我认为您可以从Activity而不是Application中实例化mCallback。
In your activity's onCreate() or onResume() method, you can write:
在您的活动的onCreate()或onResume()方法中,您可以编写:
MyApplication myApplication = getApplication();
myApplicaton.mCallback = this;
but make sure your activity have implemented OnFetchCompleteListener (I don't why you typed OnSomeItemSelectedListener in the last piece of code.)
但请确保您的活动已实现OnFetchCompleteListener(我不知道您在最后一段代码中键入OnSomeItemSelectedListener的原因。)
#1
2
I think you can instantiate mCallback from the Activity instead of from the Application.
我认为您可以从Activity而不是Application中实例化mCallback。
In your activity's onCreate() or onResume() method, you can write:
在您的活动的onCreate()或onResume()方法中,您可以编写:
MyApplication myApplication = getApplication();
myApplicaton.mCallback = this;
but make sure your activity have implemented OnFetchCompleteListener (I don't why you typed OnSomeItemSelectedListener in the last piece of code.)
但请确保您的活动已实现OnFetchCompleteListener(我不知道您在最后一段代码中键入OnSomeItemSelectedListener的原因。)