R.id. values get overwritten - android

In my Android app I'm creating new WebViews from Java code; however after I create these WebViews my previously working id's get overwritten.
Example snippet:
for (int i=0;i<mywebviewarray.length;i++){
mywebviewarray[i]=new WebView();
}
((Button)findViewById(R.id.mybutton).settext("ok");
If I run this code, I get an exception on the last line:
java.lang.ClassCastException: android.webkit.WebView
It seems to me as if the tables backing the findViewById get overwritten. I tried calling setId in the loop. but it does not help.
How can I resolve this problem?

What is in mywebviewarray. It seems to me like you have something in there that isnt a WebView and you are trying to instantiate it as a WebView.

I ended up not solving this problem, and instead of using a dynamic array of WebViews, I ended up using fixed 3 WebViews. I'm changing their content like a 3-length window over an array of URLs, thus simulating the original concept. (Albeit in a slower way)

Related

Android ViewPager2 library throwing Transactiontoolarge exception onpause event

I am getting TransactionTooLarge exception if page size = 50 and I press the home button. I checked the FragmentStateAdapter and found that 'saveState()' method is finalized.
Please help me how to resolve this.
In ViewPager it was overriden by me using below link-
https://medium.com/#mdmasudparvez/android-os-transactiontoolargeexception-on-nougat-solved-3b6e30597345
But no way in View pager2 library.
I had terrible random crash with my fragments.
The one megabyte limit is system wide, so it can crash at much lower thresholds.
I fixed the issue by stopping to use serialized objects and only passing integers into intents and fragment arguments.
Then fragments and activities can get the actual object from a repository using the integer id I gave to it.
It is faster and with insight it is a lot simpler.

Activity is getting too large and becoming more and more difficult to work with. Solutions to solve this issue?

I am working on my second Android Application, first being, hello world. The application code is quite crazy looking because I love to test new libraries and ideas in it. I have been working on this application for well over 3 months and one of my activities is getting way to large and difficult to work with. I find myself getting lost in the code and it is taking longer to do simple things. There might be simple solutions to solving this issue. I really want to split my activity into two and reference each other if possible. Is there are any suggestions to simplifying and organizing code that would be greatly helpful. Even example will help me very much.
Part of my activity is adding a ton of data into a database and the other part is a long equation with multiple values. Another part is implementing the HoloGraphLibrary (Which I love). It is also implementing a listView with custom adapter. It also has a custom dialog............ I can go on and on. I hope you get my point.
EDIT
Going to work with this.
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
Try creating classes for each responsibility.
A Database Helper that has functions to insert data too:
DatabaseHelper database = new DatabaseHelper();
database .insertData(whatever);
A HoloGraphHelper that initializes the HoloGraph
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
And so on.
Break into multiple files. First classes defined in the Activity like the adapter. Change anonymous classes to classes defined in their own file. Look for ways to break out other related code into a class.
Right click on src folder of your Project and select new - class to create a new class. You can use a class for storing methods but you won't be able to display anything on screen.
To display contents to user, you can create a new Activity bu pressing Ctrl + N and selecting Android - Android Activity.
The best way is modularise your code.
I.e split your code into various related modules, for example a separate class for each part that your testing. So you could have a database entry class, a class for Gui testing, i.e. for your custom dialog. That class does all the work for that test, into various functions, I always try to keep functions as small as possible as they are easy to read.
As an example for your database entry, you could have a function which checks the database if the record already exists and then insert it. But a better way would be your insert function only performs the insert code and instead within this function it calls CheckIfDatAlreadyExists function which can return a bool so you know whether you should go ahead and insert the record. This would keep the code tidy and clean to manage.
Then from your main activity all would need to do is instantiate the relative class and call the relevant method.

Android Minesweeper project(from codeproject.com)

Here is the link of the Minesweeper project on codeproject.com.
I just wanted to ask one thing here. I completely understood the logic and algorithm this guy used, but when he called the showMineField() method inside startNewGame() method , he called it after createMineField(). I am really confused! Shouldn't the layout be set before setting up mines and handling the user click events? But the code seems to work fine. If I just call showMineField() inside startNewGame() , it gives me NullPointerException.
This is because you cannot show a mine field before creating all the objects.
It is like trying to run without legs. If you call showMineField() that is using objects that have not been initialized. Thats why you are recieving a NUllPointerException. Nullpointer is throws when a method is trying to be call on an object that has not been created yet. createminefield() initialzes everything so that nullpointer is not thrown

MVEL2 on android throws exception

Did anyone has experience with MVEL2 on android?
i've tried out the same code with a simple java program and later on android:
The following exception is thrown when executed on android:
E/AndroidRuntime(30793): java.lang.ExceptionInInitializerError
I tried the example from the mvel website:
String template = "Hello, my name is #{name.toUpperCase()}";
Map vars = new HashMap();
vars.put("name", "Michael");
System.out.println(TemplateRuntime.eval(template, vars));
If theres no solution could anyone suggest a template engine which works with android
and supports iteration?
MVEL2 tries to substring the first 3 characters of the system java.version property when initializing the parser, and under Android the version is 0. That causes a bunch of exceptions which eventually cause the ExceptionInInitializerError.
If you want to force the java.version property, you can simply set it yourself:
System.setProperty("java.version", "1.6");
I have no idea what kind of odd side effects this may cause for Android, but at least it gets the MVEL parser up and running without throwing exceptions!
System.setProperty with "java.version" key seems to be read only propery in android, so it won't work.
i've tried to integrate MVEL 2 into android with no success, try using EVAL lib

Unable to find components using the findViewById method in Android

I'm attempting to create an Activity and unfortunately every time I want to grab one of my XML components it gives me a RunTimeException (NullPointer).
Anytime I use code such as:
TextView tv = (TextView) findViewById(R.id.myView); //I get the exception
The same happens for any components I attempt to find with that method. I can't quite figure out why. I know it isn't due to the Activity not being in the Manifest because it's the only Activity in the test app I made. (The one set up by default).
Oddly I can still use setContentView(R.id.myView). It just doesn't seem to want to find anything when using the findViewById method.
Info that might be of use:
I am currently using NetBeans as my IDE.
I have done multiple 'clean and builds' as was suggested in another question. Android -findViewById question
Has anyone run into this issue before? If so, what was the solution?
If need be, I can provide sample code of when this is happening.
Don't pass in a view ID to setContentView, pass in a layout resource ID:
setContentView(R.layout.layout_name);
If you still have problems, post your layout file.
It is very sure that you R.java is not properly generated.
Delete R.Java in netbeans IDE and Re-build the project.
Hope it resolves your query.

Categories

Resources