我注意到了一个基本调查应用中的错误,我正在努力学习安卓。
偶尔我会得到一个指向这行代码的W/System.err﹕ at MainActivity.surveyAvailable(MainActivity.java:40)
:
button.setVisibility(View.GONE);
之前我曾多次使用setVisibility
,从未遇到任何问题。
这是函数,当用户第一次进入应用程序时,以及在完成调查以检查服务器并查看是否有其他可用于用户的调查之后,将调用此函数:
public void surveyAvailable(boolean surveyIsAvailable) {
Log.d("MainActivity", "App survey is available? " + surveyIsAvailable );
Button button = (Button)findViewById(R.id.takeSurveyButton);
if (surveyIsAvailable) {
button.setVisibility(View.VISIBLE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
App.getInstance().showSurvey();
}
});
} else {
Log.d("MainActivity", "We hit here");
button.setVisibility(View.GONE);
}
}
如果调查不可用,则会记录相应的行 - App survey is available? false
和'我们点击此处'。但是按钮有时不会设置为View.GONE
而我看到System.Err行。但有时它工作正常,按钮的可见性确实会发生变化。知道怎么解决这个问题吗?或者如何获得有关System.Err实际含义的更多信息?
编辑:
我发现通过在我的活动中设置Button surveyButton;
然后将按钮引用为this.surveyButton
似乎可以使功能更符合我们所期望的(例如,当我们调用button.setVisibility(View.GONE)
视图实际上始终为GONE
)。但它仍然会抛出System.Err行,让我对事情正常工作犹豫不决。
编辑活动:
public class MainActivity extends ActionBarActivity implements SurveyListener {
Button surveyButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.surveyButton = (Button)findViewById(R.id.takeSurveyButton);
}
public void surveyAvailable(boolean surveyIsAvailable) {
Log.d("MainActivity", "App survey is available? " + surveyIsAvailable );
if (surveyIsAvailable) {
this.surveyButton.setVisibility(View.VISIBLE);
this.surveyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
App.getInstance().showSurvey();
}
});
} else {
Log.d("MainActivity", "We hit here");
this.surveyButton.setVisibility(View.GONE);
}
}
}
该活动实现了这个类:
public abstract interface SurveyListener
{
public abstract void surveyAvailable(boolean surveyAvailable);
}
主要App类,用于检查调查和调用" surveyAvailable()`:
public class App
{
private static App _instance;
private SurveyListener _eventsHandler;
private String _apiKey = "";
private String _appuserId = "";
private String _surveyUrl = "";
private Activity _parentContext;
private Boolean _surveyAvailable;
public static App initWithApiKeyAndListener(String apiKey, SurveyListener surveyEventsHandler) {
if (_instance == null)
{
_instance = new App();
_instance._parentContext = (Activity) surveyEventsHandler;
_instance.setSurveyListener(surveyEventsHandler);
_instance.setApiKey(apiKey);
String appuserId = PreferenceManager.getDefaultSharedPreferences((Activity) _instance._eventsHandler).getString(tag, "no_appuser");
if (appuserId == "no_appuser") {
_instance._surveyAvailable = true;
_instance.alertAvailability(true);
} else {
_instance.checkForCampaigns();
}
}
return _instance;
}
private void alertAvailability(boolean surveyAvailable) {
App.getInstance()._eventsHandler.surveyAvailable(surveyAvailable);
}
private void checkForCampaigns() {
new CampaignCheck().execute();
}
public static App getInstance()
{
if (_instance == null)
{
_instance = new App();
}
return _instance;
}
public void donePushed()
{
App.getInstance().checkForCampaigns();
}
private class CampaignCheck extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
Boolean surveysAvailable = false;
try {
surveysAvailable = new AppuserConnection().checkCampaigns();
App.getInstance()._surveyAvailable = surveysAvailable;
App.getInstance().alertAvailability(_surveyAvailable);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
}
答案 0 :(得分:1)
您不应该从其他线程修改UI元素。您是通过在后台线程上调用App.getInstance().alertAvailability(_surveyAvailable);
来完成此操作的。将其移至AsyncTask的onPostExecute
。