Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使我的应用程序不挂在设备上_Java_Android_Multithreading_Arraylist_Android Asynctask - Fatal编程技术网

Java 如何使我的应用程序不挂在设备上

Java 如何使我的应用程序不挂在设备上,java,android,multithreading,arraylist,android-asynctask,Java,Android,Multithreading,Arraylist,Android Asynctask,我对android开发和软件编程基本上是新手,我相信我的应用程序中存在线程问题。该应用程序所做的是基于对api的两个查询搜索两组结果,并将每组结果存储在自己的列表中。将生成一个新列表,其中仅包含两个列表中的元素。该应用程序在我桌面上的虚拟设备上运行,但挂在我的Galaxy Nexus上。我正在为此使用arraylist,但我想知道hashset是否能够更快地完成这种类型的操作。以下是我的主要活动。getfirst和secondID以及getfirst和secondtitle在asynctask中

我对android开发和软件编程基本上是新手,我相信我的应用程序中存在线程问题。该应用程序所做的是基于对api的两个查询搜索两组结果,并将每组结果存储在自己的列表中。将生成一个新列表,其中仅包含两个列表中的元素。该应用程序在我桌面上的虚拟设备上运行,但挂在我的Galaxy Nexus上。我正在为此使用arraylist,但我想知道hashset是否能够更快地完成这种类型的操作。以下是我的主要活动。getfirst和secondID以及getfirst和secondtitle在asynctask中完成,以防止networkonmainthread异常。这是执行此应用程序的最佳方式吗?谢谢你的帮助

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.totlayout);

    //set the UI elements
    searchOne = (EditText) findViewById(R.id.searchOne);
    searchTwo = (EditText) findViewById(R.id.searchTwo);

    findMovies = (Button) findViewById(R.id.findMovies);

    searchOne.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            //make person search url1
            final StringBuilder personSearchURLOne = new StringBuilder(getName.getName1(searchOne)); 
            searchURLOne = personSearchURLOne.toString();
            return false;
        }
    });

    searchTwo.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            //make person search url2
            final StringBuilder personSearchURLTwo = new StringBuilder(getName.getName2(searchTwo));
            searchURLTwo = personSearchURLTwo.toString();

            return false;
        }
    }); 
}
public void getData(String searchURLOne, String searchURLTwo){
    try{
        //get ID 1 
        idOne = new getFirstID().execute(searchURLOne).get();
        Log.d("JSONArray idOne", idOne.toString());

       //get ID 2
       idTwo = new getSecondID().execute(searchURLTwo).get();
       Log.d("JSONArray idTwo", idTwo.toString());

       //make credit search url1
       final StringBuilder creditURLOne = new StringBuilder(buildCreditURL.getCreditURLOne(idOne));
       final String creditOne = creditURLOne.toString(); 
       Log.d("creditOne contains", creditOne);

       //make credit search url2
       final StringBuilder creditURLTwo = new StringBuilder(buildCreditURL.getCreditURLTwo(idTwo));
       final String creditTwo = creditURLTwo.toString();

       //get array of tiles for credit url 1 
       titleOne = new getFirstTitle().execute(creditOne).get();
       Log.d("titleOne Contains", titleOne.toString());

       //get array of titles for credit url 2
       titleTwo = new getSecondTitle().execute(creditTwo).get();

       //parse out common films into new array 
       myCommonFilms = new ArrayList<String>(commonFilms.getCommonFilms(titleOne, titleTwo));
    }
    catch(InterruptedException e){
        e.printStackTrace();
    }catch(ExecutionException e){
        e.printStackTrace();
    }



}
public void displayResults(View view) throws InterruptedException, ExecutionException{
    //do something in response to button
    getData(searchURLOne, searchURLTwo);
    Intent intent = new Intent(this, DisplayResultsActivity.class).putStringArrayListExtra("myCommonFilmsList", myCommonFilms);
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.totlayout, menu);
    return true;
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.totlayout);
//设置UI元素
searchOne=(EditText)findViewById(R.id.searchOne);
searchTwo=(EditText)findViewById(R.id.searchTwo);
findMovies=(按钮)findViewById(R.id.findMovies);
searchOne.setOnKeyListener(新视图.OnKeyListener(){
@凌驾
公共布尔onKey(视图v、int keyCode、KeyEvent事件){
//TODO自动生成的方法存根
//让人搜索url1
最终StringBuilder personSearchURLOne=新StringBuilder(getName.getName1(searchOne));
searchURLOne=personSearchURLOne.toString();
返回false;
}
});
searchTwo.setOnKeyListener(新视图.OnKeyListener(){
@凌驾
公共布尔onKey(视图v、int keyCode、KeyEvent事件){
//TODO自动生成的方法存根
//让人搜索url2
最终StringBuilder personSearchURLTwo=新StringBuilder(getName.getName2(searchTwo));
searchURLTwo=personSearchURLTwo.toString();
返回false;
}
}); 
}
public void getData(字符串searchURLOne、字符串searchURLTwo){
试一试{
//获取ID 1
idOne=new getFirstID().execute(searchURLOne.get();
Log.d(“JSONArray idOne”,idOne.toString());
//获取ID 2
idTwo=new getSecondID().execute(searchURLTwo.get();
d(“JSONArray idTwo”,idTwo.toString());
//进行信用搜索url1
最终StringBuilder creditURLOne=新的StringBuilder(buildCreditURL.getCreditURLOne(idOne));
最后一个字符串creditOne=creditURLOne.toString();
Log.d(“creditOne包含”,creditOne);
//进行信用查询url2
final StringBuilder creditURLTwo=新的StringBuilder(buildCreditURL.getCreditURLTwo(idTwo));
最后一个字符串creditTwo=creditURLTwo.toString();
//获取信用url 1的磁贴数组
titleOne=new getFirstTitle().execute(creditOne.get();
Log.d(“titleOne包含”,titleOne.toString());
//获取信用卡url 2的标题数组
titleTwo=new getSecondTitle().execute(creditTwo.get();
//将普通胶片解析为新阵列
myCommonFilms=新阵列列表(commonFilms.getCommonFilms(titleOne,titleTwo));
}
捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
}
public void displayResults(视图)引发InterruptedException、ExecutionException{
//对按钮做些反应
getData(searchURLOne、searchURLTwo);
Intent Intent=新的Intent(这个,DisplayResultsActivity.class);
星触觉(意向);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.totlayout,menu);
返回true;
}
}

我认为这是正确的方法:使用AsyncTask是防止应用程序冻结UI的简单方法

但我真的不明白下面这句话的意思

idOne = new getFirstID().execute(searchURLOne).get();
事实上,我以前从未见过。不管怎样,您在ArrayList中管理多少条记录?10, 100, 1000, 1 000 000 ?


如果您使用的是大量的记录,那么您的手机将需要一定的时间来完成操作,并且您无法减少它。如果集合太大,唯一的解决方案是尝试直接在服务器上或在构建ArrayList时减少它。

我不一定关注AsyncTask的创建位置并付诸实施,但将整个getData方法放在AsyncTask上似乎是个好主意。除此之外,如果它能在和上工作,但不能在设备上工作,我的第一个怀疑就是网络访问。确保您的设备不仅可以访问internet,还可以访问运行远程查询所需的服务。人们经常误解,手机看不到与台式机相同的主机。此外,如果您的手机在Wifi上漫游,请检查Wifi网络是否允许访问该服务。远程查询服务是web服务吗?您尝试运行的是DBMS SQL调用吗?查询是否需要特定的网络端口?

此端口似乎会阻塞UI线程:

   titleOne = new getFirstTitle().execute(creditOne).get();
最好使用一个新线程来完成长时间的工作,完成后,将新数据通知UI线程

您可以使用asyncTask使其更简单。如果您更喜欢普通线程,请使用(and use)或在线程完成后使用

如果您使用hanlder,不要忘记在线程之外创建它


作为一名新的android开发者,你应该观看谷歌IO视频。他们能帮上大忙。观看2010年至2012年的视频

在getData中,有对getFirstID和getSecondID以及getSecondTitle和getSecondTitle的调用。这些类实现asynctask并查询api。我想尝试使用getData扩展asynctask,但无法找到在asynctask接受字符串时传入两个参数的方法。。。在我的研究和实践中