我正在寻找一个解决方案,以更改AsyncTask中的自定义视图的可见性。我已阅读本:在AsyncTask中更改自定义视图的可见性
How to handle visibility changes for a custom android view/widget
但它并不能帮助我,因为我不明白如何处理接口。我已经把它放在我的自定义视图类中,但我在哪里注册监听器?在我有我的AsyncTask的活动中?那么onSetVisibilityCalled()
方法应该怎么做?
我希望你能帮助我,我感谢你的帮助。
代码片断:
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);
}
}
}
如果我去掉myCustomView.setVisibility(View.GONE);在onPreExecute() - 方法我收到以下错误:
04-02 16:04:47.016:E/AndroidRuntime(322):致命异常:主要04-02 16:04:47.016:E/AndroidRuntime(322):java.lang.RuntimeException: 无法启动活动 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) ): 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-02E/AndroidRuntime(322):at16:04:47.016:E/AndroidRuntime(322):at java.lang.reflect.Method.invoke(Method.java:525)04-02 16:04:47.016: (0)收起该表格展开该表格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):导致: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):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257) 04-02 16:04:47.016:E/AndroidRuntime(322):11 ...更多
只需在'onPreExecute()'和'onPostExecute()'中取消'myCustomView.setVisibility(..)'行的注释。这应该工作。 – Drew
我做到了,我编辑了我的问题,并在我的LogCat中显示错误。我希望他们告诉你一些事情。 – Lyte
是的,他们做到了。检查答案 – Drew