Android layout 活动开关和异步Android

Android layout 活动开关和异步Android,android-layout,android-asynctask,android-activity,android-view,background-process,Android Layout,Android Asynctask,Android Activity,Android View,Background Process,我有两个活动:main活动与main.xmllayout(应用程序“主页”屏幕)关联,以及AboutActivity与about.xmllayout(应用程序“about”屏幕)关联 在AboutActivity中,main活动中的Async任务仍尝试访问main.xml。我的应用程序因此停止工作 我有办法吗 在MainActivity中“暂停”Async任务,并在 用户从关于触觉的切换回 或者仍然在后台访问main.xml,而在AboutActivity 其他信息: main活动是启动活动

我有两个活动:
main活动
main.xml
layout(应用程序“主页”屏幕)关联,以及
AboutActivity
about.xml
layout(应用程序“about”屏幕)关联

AboutActivity
中,
main活动中的
Async
任务仍尝试访问
main.xml
。我的应用程序因此停止工作

我有办法吗

  • MainActivity
    中“暂停”
    Async
    任务,并在 用户从关于触觉的
    切换回
  • 或者仍然在后台访问
    main.xml
    ,而在
    AboutActivity
其他信息:
main活动
是启动活动<代码>关于触觉扩展主活动
。用户可以使用选项菜单进入“关于”屏幕/切换到
AboutActivity

main活动中的
Async
任务将用户的当前位置放入文本视图
about.xml
仅包含静态文本
AboutActivity
只显示
about.xml

关于触觉:

public class AboutActivity extends MainActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.about);

    }

}
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            // Creating a new non-ui thread task to download Google place json data 
            PlacesTask placesTask = new PlacesTask();                                   

        // Invokes the "doInBackground()" method of the class PlaceTask
            placesTask.execute(sb.toString());
        }

        /** A class, to download Google Places */
    private class PlacesTask extends AsyncTask<String, Integer, String>{

        String data = null;

        // Invoked by execute() method of this object
        @Override
        protected String doInBackground(String... url) {
            //make asynctask wait for debugger
            //android.os.Debug.waitForDebugger();

            try{
                data = downloadUrl(url[0]);
            }catch(Exception e){
                 Log.d(DEBUG,e.toString());
            }
            return data;
        }

        // Executed after the complete execution of doInBackground() method
        @Override
        protected void onPostExecute(String result){            
                TextView curLoc = (TextView) findViewById(R.id.CurrentLocation);
                curLoc.setText(result);
        }

    }
}
主要活动:

public class AboutActivity extends MainActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.about);

    }

}
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            // Creating a new non-ui thread task to download Google place json data 
            PlacesTask placesTask = new PlacesTask();                                   

        // Invokes the "doInBackground()" method of the class PlaceTask
            placesTask.execute(sb.toString());
        }

        /** A class, to download Google Places */
    private class PlacesTask extends AsyncTask<String, Integer, String>{

        String data = null;

        // Invoked by execute() method of this object
        @Override
        protected String doInBackground(String... url) {
            //make asynctask wait for debugger
            //android.os.Debug.waitForDebugger();

            try{
                data = downloadUrl(url[0]);
            }catch(Exception e){
                 Log.d(DEBUG,e.toString());
            }
            return data;
        }

        // Executed after the complete execution of doInBackground() method
        @Override
        protected void onPostExecute(String result){            
                TextView curLoc = (TextView) findViewById(R.id.CurrentLocation);
                curLoc.setText(result);
        }

    }
}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建新的非ui线程任务以下载Google place json数据
PlacesTask PlacesTask=新的PlacesTask();
//调用类PlaceTask的“doInBackground()”方法
执行(sb.toString());
}
/**一个类,下载谷歌的地方*/
私有类PlacesTask扩展异步任务{
字符串数据=null;
//由此对象的execute()方法调用
@凌驾
受保护的字符串doInBackground(字符串…url){
//使异步任务等待调试器
//android.os.Debug.waitForDebugger();
试一试{
数据=下载url(url[0]);
}捕获(例外e){
d(DEBUG,例如toString());
}
返回数据;
}
//在完全执行doInBackground()方法后执行
@凌驾
受保护的void onPostExecute(字符串结果){
TextView curLoc=(TextView)findViewById(R.id.CurrentLocation);
curLoc.setText(结果);
}
}
}

AsyncTask不暂停,这样我们可以在任务完成和启动后台活动时临时存储数据,而不是在textView上显示数据

我已经解决了这个问题。这部分是一个可变范围的问题,部分是因为我不能在那里使用
findViewById()
,它总是返回null

curLoc
在异步任务的onPostExecute中为空

我删除了以下内容:

TextView curLoc = (TextView) findViewById(R.id.CurrentLocation);
并将curLoc声明为类MainActivity的属性

private TextView curLoc;
并在onCreate()中输入