简体   繁体   English

如何使我的应用无法挂在设备上

[英]How do I get my app to not hang on device

I am new to android development and software programming in general and believe I have a threading issue in my app. 我通常是android开发和软件编程的新手,并且相信我的应用程序中存在线程问题。 What the app does is searches for two sets of results based on two queries to an api and stores each set of results in its own list. 该应用程序的作用是基于对api的两次查询来搜索两组结果,并将每组结果存储在其自己的列表中。 A new list is generated containing only the elements that are in both lists. 生成一个仅包含两个列表中的元素的新列表。 The app runs in a virtual device on my desktop but hangs on my Galaxy Nexus. 该应用程序在桌面上的虚拟设备中运行,但挂在我的Galaxy Nexus上。 I am using arraylist for this but I am wondering if perhaps hashset would be faster at accomplishing this type of operation. 我为此使用arraylist,但我想知道散列集是否可以更快地完成这种类型的操作。 Below is my main activity. 以下是我的主要活动。 getfirst and secondID are done in an asynctask as well as getfirst and secondtitle in order to prevent networkonmainthread exception. getfirst和secondID以及getfirst和secondtitle都在asynctask中完成,以防止networkonmainthread异常。 Is that the best way to thread this application? 这是线程化此应用程序的最佳方法吗? Thanks for any help. 谢谢你的帮助。

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

I think it's the correct way : using AsyncTask is an easy way to prevent your app for freezing the UI. 我认为这是正确的方法:使用AsyncTask是防止您的应用冻结UI的简单方法。

But I don't really understand what the following line do. 但是我不太明白下面这行的内容。

idOne = new getFirstID().execute(searchURLOne).get();

Actually, I never see it before. 实际上,我从未见过。 Anyway, how many records are you managing in your ArrayList ? 无论如何,您要在ArrayList中管理多少记录? 10, 100, 1000, 1 000 000 ? 10,100,1000,1 000 000?

If you're working with a lot of records, your phone will take a certain amount of time to accomplish the operations and you'll not be able to reduce it. 如果您要处理大量记录,那么您的电话将花费一定的时间来完成操作,并且您将无法减少它。 The only solution, if the set is too large, is to trying to reduce it maybe directly on your server or when you're building your ArrayList. 如果集合太大,唯一的解决方案是尝试直接在您的服务器上或在构建ArrayList时减少它。

I'm not necessarily following where the AsyncTask is created and put into action but it seems like it would be a good idea to put the entire getData method on the AsyncTask. 我不一定要跟随AsyncTask的创建位置并付诸实践,但是将整个getData方法放在AsyncTask上似乎是一个好主意。 That aside, if it works on the and but not on the device my 1st suspicion would be network access. 顺便说一句,如果它能在而不是在设备上运行,我的第一怀疑是网络访问。 Make sure your device has access not only to the internet but to the service that it needs to run the remote query on. 确保您的设备不仅可以访问互联网,还可以访问运行远程查询所需的服务。 It's very common to misunderstand that phones cannot see the same host computers as our desktops can. 经常会误解电话无法看到与台式机相同的主机。 Also, if you're phone is roaming on Wifi check that the Wifi network will allow the service to be accessed. 另外,如果您的手机正在Wifi上漫游,请检查Wifi网络是否可以访问该服务。 Is the remote query service a web service? 远程查询服务是Web服务吗? Is it a DBMS SQL call you are trying to run? 您要运行的是DBMS SQL调用吗? does the query require a specific network port? 查询是否需要特定的网络端口?

this one seems to block the UI thread: 这个似乎阻塞了UI线程:

   titleOne = new getFirstTitle().execute(creditOne).get();

better use a new thread for doing the long job , and once it finishes , notify the UI thread about the new data . 最好使用新的线程进行长时间的工作,一旦完成,通知UI线程有关新的数据。

you can use asyncTask to make it easier for you . 您可以使用asyncTask使您更轻松。 if you prefer the normal threads ,either use a handler (and use handler.post ) or use runOnUiThread once the thread has finished. 如果你喜欢正常的线程,要么使用一个handler (并使用handler.post ),或使用runOnUiThread一旦线程完成。

if you use a hanlder , don't forget to create it outside the of thread itself. 如果您使用hanlder,请不要忘记在线程本身的外部创建它。

as a new android developer , you should watch the google IO videos . 作为新的android开发人员,您应该观看google IO视频。 they can help a lot . 他们可以提供很多帮助。 watch videos from 2010 to 2012 . 观看2010年至2012年的视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取计算机的设备令牌? - How do i get a device token of my computer? 关闭应用程序时如何断开与设备的连接? - How do I disconnect from a device when I close my app? 如何在Android App中获得CPU调速器? - How do I get my CPU Governor in an Android App? 如何让 Firebase 电话身份验证适用于我的应用? - How do I get Firebase Phone Authentication to work for my app? JAVA桌面应用程序挂起时该怎么办? - What should I do when JAVA desktop app hang on? 如何从连接到我的Android设备的串行GPS获取NMEA数据? - How do I get the NMEA data from a serial GPS connected to my android device? 如何让 BluetoothAdapter.startDiscovery() 在我的 Android 10 设备上工作? - How do I get the BluetoothAdapter.startDiscovery() working on my Android 10 device? 如何在我的PC上获取我的应用程序(Android)存储的文件? - How do I get the files I stored with my app (Android) on my PC? 如何让我的 Java 应用程序像状态应用程序一样从 MacOS 桌面菜单栏运行? - How do I get my Java App to run from the MacOS desktop menu bar like a status app? 我的程序已挂起,暂时不知道如何修复 - My program has hang and I dont know how to fix it for now
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM