0

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

Community
  • 1
  • 1
Lyte
  • 58
  • 1
  • 9

2 Answers2

1

Your problem is not in AsyncTask. In your onCreate(Bundle) method you did not setContentView(int).

Assuming that your ProgressBar and MyCustomView are defined in your_activity_layout.xml resource file, you should add a line: setContentView(R.layout.your_activity_layout.xml).

Add it just after the call to super(Bundle) constructor (you forgot to add it,too):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super(savedInstanceState);//you forgot that
    setContentView(R.layout.your_activity_layout.xml);//and that
    myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
    myCustomView = (MyCustomView) findViewById(R.id.myCustomView);
    ...
}

Now both of your Views should be not null (if they present in xml layout file), and the above code should work.

Drew
  • 3,307
  • 22
  • 33
  • Wow, I am so sorry... I forgot to put in this two lines in the code snippet here. I have them and the errors are the same. :/ – Lyte Apr 02 '14 at 17:44
  • Could it be something because of the order Activities/Views are created in Android? Maybe the View does not exist when I try to change the visibility. – Lyte Apr 02 '14 at 17:46
  • They definitely don't exist, that's what my answer points to. Particularly, if your real code is the same as the code you provided in your post, then the source of an error is your ProgressBar. Double check the id you provided to `findViewById` and the id specified in xml file. If you have `R.id.myProgressBar` in Activity, then in XML you should have a tag: `` – Drew Apr 02 '14 at 18:01
  • If the IDs are correct, try cleaning and building your project once again - just to make sure the R.java contains all the necessary IDs. – Drew Apr 02 '14 at 18:04
  • Thank you for your help! I managed to fix it... the id's were all correct! I now created a public and static class with some attributes for collecting data and a boolean. If my calculations are done I hide the ProgressBar and draw everything. If they're not done (I check it in the onDraw()) I just call invalidate(). I didn't find any solution for my problem, so I use this untill I find something better. I dont think my solution is very well... there are just too many unnecessary invalidate()-calls. :( – Lyte Apr 03 '14 at 17:04
0

you do not need to handle visibility in this way.if you just need to change visibility just call yourview.setVisibility and if you want to be informed if this called override setVisibility in your custom view and call super.setVisibilty at first then do want you need to do.

sadegh saati
  • 1,168
  • 1
  • 13
  • 24
  • I'm a little bit confused. How did I change then the visibility of a ProgressBar in my AsyncTask? I initialized the ProgressBar in the onCreate() of the Activity and later, in the AsyncTask, I change the visibility. – Lyte Apr 02 '14 at 13:22
  • if you initialized it within onCreate so pass it to your AsyncTask then you can change the visibility there. you can post what you've done then I'll help you. – sadegh saati Apr 02 '14 at 13:27
  • @mohammadsadeghsaati **Your answer is completely wrong**. `AsyncTask` does not run on a separate Thread all the time. For example, methods `onPostExecute` and `onPreExecute` **get invoked on UI thread**. You should modify or remove your post in order not to confuse android students. – Drew Apr 02 '14 at 13:57
  • According to stacktrace your problem is NullPointerException. There is no problem in the code. – sadegh saati Apr 02 '14 at 16:45
  • All ids are correct. :/ I tried something new... I created a reference of the View in my Activity and a Reference of the View in my AsyncTask. The constructor of the AsyncTask initializes his reference to the View with the reference of the View in the Activity. I tried then to call postInvalidate() on the reference in my onPostExecute()-method, but I still get a NullPointerException. – Lyte Apr 03 '14 at 13:48