1

i have like thousands of html files in my assets directory and i have to store their position in the java file but there is a limit of stack space in java which is not allowing me do so i increased my stack memory by doing this http://wiki.eclipse.org/FAQ_How_do_I_increase_the_heap_size_available_to_Eclipse%3F

but now im able to paste my code but after few seconds i again get stackoverflow error and it prompts me to terminate workspace

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewActivity extends Activity {
    WebView web;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        web = (WebView) findViewById(R.id.webView1);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        int pos = getIntent().getIntExtra("key",0);
 if(pos==0){    web.loadUrl("file:///android_asset/1.html");
else if(pos==1){    web.loadUrl("file:///android_asset/2.html");}   
else if(pos==2){    web.loadUrl("file:///android_asset/3.html");}       
else if(pos==3){    web.loadUrl("file:///android_asset/4.html");}        
else if(pos==4){    web.loadUrl("file:///android_asset/5.html");}

      // similarly for 4 and 5 and so on.
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

        }
    }
}
Anonymous
  • 1,389
  • 3
  • 11
  • 16

2 Answers2

0

Sounds like you shouldn't be doing that at all. If you're running out of stack, that's not a very normal occurrence (unless you're doing infinite recursion). Running out of heap would be somewhat more common.

You're saying that you have thousands of html files, and you're storing their position in a java file. That sounds like really bad design. The positions should be stored in a database, or if that's not possible (or rather, you don't want to), an external file that you read in.

Can you paste a few lines of the code you're having trouble with, and explain what you intend to do with those thousands of HTML files, and we'll give you a proper solution.

Edit: Okay. A proper solution would be to store the URLs into the SQLite database, with "pos" as the primary key. Then you'll need only a single line of code, i.e. something like

web.loadUrl(sqlDb.load("select url FROM mytable where pos = " + pos));

There's a tutorial here: AndroidSQLite

Good luck! :)

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • ok ill post my positions , is there any way to store the positions (else if Statements ) in external file? like you said – Anonymous Dec 10 '13 at 07:49
  • What do you mean by positions? Where are you actually stuck? How far have you gotten? – nitind Dec 10 '13 at 07:51
  • Don't worry about it. Just post your things and we'll tell you how to do things right (i.e. without thousands of else if statements). – Kayaman Dec 10 '13 at 07:53
  • Lars Vogel's tutorials are even bigger than my code lol can you please give me an example of doing it? i.e how to refer it to the sql database? how to add html files in sql database? – Anonymous Dec 10 '13 at 08:06
  • To be honest my Android-fu isn't so strong that I could write it off the top of my head, and I don't have time to start reading that tutorial myself now. – Kayaman Dec 10 '13 at 08:09
  • how do i create a new sql thing in eclipse??? – Anonymous Dec 10 '13 at 08:16
  • Now is when you should be spending a lot more time reading and a lot less time writing. I'm assuming that this is a school assignment, although if it is, I'm surprised that you have "thousands of html files". Of course if you don't know anything about databases, you have quite a bit to learn. Google is your friend though, I bet you'll be able to find plenty of examples. – Kayaman Dec 10 '13 at 08:19
0

Another advice:

if(pos==0){    web.loadUrl("file:///android_asset/1.html");
else if(pos==1){    web.loadUrl("file:///android_asset/2.html");}   
else if(pos==2){    web.loadUrl("file:///android_asset/3.html");}       
else if(pos==3){    web.loadUrl("file:///android_asset/4.html");}        
else if(pos==4){    web.loadUrl("file:///android_asset/5.html");}

I do not know how this code is representative for what you're really doing, but here it looks like you can compute the URL:

String url = "file:///android_asset/" + (pos+1) + ".html";

OTOH, this is not code that could possibly result in a stack overflow. But when it does, increase the stack size, not the heap size. Look, if your car is out of fuel, you add gas, not water for the wind-shield washer.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • how do i icrease the stack size? – Anonymous Dec 10 '13 at 08:54
  • Read @Matt Bulls comment above. It is the -Xss JVM option. You should get a basic understanding of the tools you use as programmer. For java programmers, the JVM is the most important tool. – Ingo Dec 10 '13 at 08:58
  • i did that , i cannot find any "-Xss" thing so i added "-Xss900m" in my ini file and after that, eclipse doesnot launch – Anonymous Dec 10 '13 at 09:01
  • Try -Xss4m, it should be enough. – Ingo Dec 10 '13 at 09:02
  • its letting me paste the code but now it says "The code of method onCreate(Bundle) is exceeding the 65535 bytes limit" – Anonymous Dec 10 '13 at 09:06
  • Have you replaced the `if .. else if ..` code according to my advise? It sounds like you didn't and you won't be able to have that much code in a single method. – Ingo Dec 10 '13 at 09:10
  • i already did that yesterday but the problem is it is opening wrong activity see my question here http://stackoverflow.com/questions/20487851/searchbar-changing-positions-due-to-filtering – Anonymous Dec 10 '13 at 09:12
  • is there any way of increasing the 65536 byte limit?? – Anonymous Dec 10 '13 at 09:15
  • Sorry, but "it opens wrong activity" makes no sense for me. Who is "it"? By the way, this already looks like a follow-up question. – Ingo Dec 10 '13 at 09:16
  • please read the question and you will get to know – Anonymous Dec 10 '13 at 09:22