更改AsyncTask中自定义视图的可见性

时间:2014-04-02 13:01:31

标签: android android-asynctask android-custom-view

我正在寻找一个关于在AsyncTask中更改自定义视图的可见性的解决方案。我读过这个:

How to handle visibility changes for a custom android view/widget

但它并没有帮助我,因为我不知道如何处理界面。我把它放在我的自定义视图类中,但是我在哪里注册了监听器?在我有AsyncTask的Activity中?那么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):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):引起: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

2 个答案:

答案 0 :(得分:1)

您的问题不在AsyncTask中。 在onCreate(Bundle)方法中,您没有setContentView(int)

假设您的ProgressBar和MyCustomView在 your_activity_layout.xml 资源文件中定义,您应该添加一行:setContentView(R.layout.your_activity_layout.xml)

在调用super(Bundle)构造函数后添加它(您也忘了添加它):

@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);
    ...
}

现在你的View都不应该为空(如果它们出现在xml布局文件中),上面的代码应该可以工作。

答案 1 :(得分:0)

您不需要以这种方式处理可见性。如果您只需要更改可见性,只需调用yourview.setVisibility,如果您希望在自定义视图中调用此覆盖setVisibility,并且首先调用super.setVisibilty你想要你做什么。