I am searching for a solution on changing the visibility of my custom view within an AsyncTask. I have read this:
How to handle visibility changes for a custom android view/widget
But it doesn't help me since I don't get how to handle the Interface. I've put this in my Custom View Class, but where do I register the listener? In the Activity where I have my AsyncTask? And what should the method onSetVisibilityCalled()
do then?
I hope you can help me and I appreciate your help.
Code snippet:
public Class MyActivity extends Activity{
private ProgressBar myProgressBar;
private MyCustomView myCustomView;
private MyTask myTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super(savedInstanceState);
setContentView(R.layout.my_activity_layout.xml);
myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
myCustomView = (MyCustomView) findViewById(R.id.myCustomView);
myTask = new MyTask();
myTask.execute();
}
private class MyTask extends AsyncTask<Void, Void, Void>{
protected void onPreExecute(){
myProgressBar.setVisibility(View.VISIBLE);
// Here I would like to change the visibility of my Custom View to GONE
// myCustomView.setVisibility(View.GONE);
}
@Override
protected void doInBackground(Void... params){
//... do something
}
protected void onPostExecute(){
myProgressBar.setVisibility(View.GONE);
// Here I would like to change the visibility of my Custom View to VISIBLE
// myCustomView.setVisibility(View.VISIBLE);
}
}
}
If i uncomment myCustomView.setVisibility(View.GONE); in the onPreExecute()-Method I get following errors:
04-02 16:04:47.016: E/AndroidRuntime(322): FATAL EXCEPTION: main 04-02 16:04:47.016: E/AndroidRuntime(322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MyActivity}: java.lang.NullPointerException 04-02 16:04:47.016: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.app.ActivityThread.access$700(ActivityThread.java:159) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:176) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:5419) 04-02 16:04:47.016: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method) 04-02 16:04:47.016: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:525) 04-02 16:04:47.016: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 04-02 16:04:47.016: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 04-02 16:04:47.016: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method) 04-02 16:04:47.016: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException 04-02 16:04:47.016: E/AndroidRuntime(322): at com.example.test.MyActivity$FetchLocationTask.onPreExecute(MyActivity.java:55) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.os.AsyncTask.execute(AsyncTask.java:534) 04-02 16:04:47.016: E/AndroidRuntime(322): at com.example.test.MyActivity.onCreate(MyActivity.java:32) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.app.Activity.performCreate(Activity.java:5372) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 04-02 16:04:47.016: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257) 04-02 16:04:47.016: E/AndroidRuntime(322): ... 11 more