Tried to make it short but have to explain in detail. I have one small project where I have one activity and multiple fragments. While launching, first fragment added and works fine. After that when I navigated to another fragment by using replace, another fragment opens but it does not display any UI at all. When i debugged it, then while inflating it says Resource not found without any extra details. While in log it's Invalid ID XXXXXXXXX where XXXXXXXXX is the id number but I am not able to find which id it is because as there is no R file in android studio.
Also tried to use analyze apk but did not find any id with that number there as well with no luck.
Also tried clean build, with no luck.
Please do help here.
I've seen this kind of errors many times here in Stack Overflow. It may caused by passing an invalid/wrong resource id e.g. R.id instead of R.layout but most the time it because of non-existing ids like following example:
int value = 999;
Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
Even though the code compiles just fine, but as you already know it will throw a RuntimeException.
Anyway, you can check your ids with the following code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StringBuilder sb = new StringBuilder();
Class clazz = R.class;
Class[] classes = clazz.getDeclaredClasses();
for(int m=0; m < classes.length; ++m) {
sb.append("Class name: " + classes[m].getSimpleName() + "\n");
Field[] fields = classes[m].getDeclaredFields();
sb.append("Number of fields: " + fields.length + "\n");
try {
for(int n=0; n < fields.length; ++n)
sb.append(String.format("Field[%d]: %s=0x%08x\n", n+1, fields[n].getName(), fields[n].getInt(fields[n])));
} catch(Exception e) {}
}
Log.d("TAG", sb.toString());
//Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
looks for me like your app doesn't have a layout file. It only shows something if you have a Layout file that tells Android Studio how the fragment is supposed to look like. Make in the Layout folder a document for it and connect it to the fragment.
you can see I have for every single of the classes/Fragnments a Layout file that tells Android Studio how to show every part of the class, shape and color of buttons, TextViews and more.
You then connect it to the class so the class knows where it's layout is
I hope that is going to help you
Related
I'm currently creating a soundboard app. I have about 100+ sound files to import.
I have code lines (android:onClick="song1") and (MediaPlayer mysound1).
Just wondering if there is a way to copy+paste these lines and have android studio auto change the line to "song2" and "song3" all the way to "song100"? Same goes for the "mysound1" all the way to "mysound100". I hope I do not have to do it manually :(
Thank you!
The approach you're using seems like a brute force method. There are much more elegant ways to approach this. For one, I would recommend creating dynamic Android components programmatically instead of in XML. Then you could hold all of your elements in a List or a Map, and you could tie them to a generic onClick listener. I'd recommend taking a look around online to figure out how to do some of these things.
But, if you would like to stick with you original method, I don't believe that Android has a way to auto number your click events. You could, however, write code to write your code. Here's a simple example using Java - since you're writing an Android app (utilizing the Eclipse console to print out to):
public class WriteMyCode {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= 100; i++) {
sb.append("<Button android:id=\"#+id/mybutton\"\n");
sb.append(" android:layout_width=\"wrap_content\"\n");
sb.append(" android:layout_height=\"wrap_content\"\n");
sb.append(" android:text=\"Click me!\"\n");
sb.append(" android:onClick=\"" + "song" + i + "\" />\n\n");
}
System.out.println(sb.toString());
}
}
You can then copy the output from the Eclipse console and paste it into your Android XML resource file.
I went to use Microsoft Excel to auto fill the numbers all the way to 100. Then CONCATENATE the ; to the end of "Mediaplayer mysound1".
I want to integrate text widget in my application in android studio. I tried running the samples and they are working fine. Now i want to toast the digital text that appears after user input. I want to save that text somehere. I used this code in OnRecognitionEnd() function
#Override
public void onRecognitionEnd() {
String Result = mEditText.getText().toString();
Log.d(TAG, "result is: " +Result);
}
Now can you kindly tell me I want to display the text that is being stored in “Result” I used multiple ways but it says UNFORTUNETLY THE APPLICATION HAS STOPPED. For Example i simply did this
TextView textElement = null;
textElement.setText(Result);
Please tell me what I am doing wrong what should i do to display it ?
If you do this :
TextView textElement = null;
textElement.setText(Result);
You will try to setText on null. I suppose the error in your logcat is NullPointerException.
You have to instantiate your textElement.
TextView textElement=(TextView)findViewById(R.id.myid);
Where "myid" is the id in your layout.xml.
I'm pretty new with Appium. I tried robotium blackbox way with .apk file to fill up a small web-view form which is auto injected by other rails server and every thing is working fine for me but
When i tried to click on Save & Next button it clicks on the edit-text box in which my previous entry filed through script.
I used all the way
solo.waitForText("SaveAndNext");
solo.clickOnWebElement(By.id("SaveAndNext"));
solo.clickOnWebElement(By.name("Save & Next"));
solo.clickOnWebElement(By.textContent("Save & Next"));
But it click on the edittext box.
Here is my code :-
solo.waitForActivity("ViewQuestions");
getInstrumentation().waitForIdleSync();
solo.clickOnText("(?i).*?Yes.*");
solo.enterTextInWebElement(By.className("text_answer"), "2");
solo.hideSoftKeyboard();
solo.waitForText("SaveAndNext");
//solo.clickOnWebElement(By.id("SaveAndNext"));
//solo.clickOnWebElement(By.name("Save & Next"));
//solo.clickOnWebElement(By.textContent("Save & Next"));
for (WebElement webElement : solo.getCurrentWebElements()) {
Log.d("Robotium", "id: " + webElement.getId() + " textContent: "
+ webElement.getTagName());
if (webElement.getId() == "SaveAndNext") {
solo.clickOnWebElement(By.id("SaveAndNext"));
}
}
I have checked that if (webElement.getId() == "SaveAndNext") is found passed.
And in logcat
**Robotium id: SaveAndNext textContent: INPUT**
is shown.
Any help will be appreciate.
Remove this for loop and use just:
solo.clickOnWebElement(By.id("SaveAndNext"));
Btw you cannot compare Strings like:
webElement.getId() == "SaveAndNext"
You should rather use equals:
"SaveAndNext".equals(webElement.getId())
I had a similar problem with view clicks were not proper. Got this solution from robotium.org
Why do text and button clicks get wrong?
If this problem happens on one of the supported versions then try to add this tag to the test project's AndroidManifest.xml
uses-sdk android:targetSdkVersion="YOUR_VERSION"
Where YOUR_VERSION is 6 for Android 2.0, 7 for Android 2.1 and 8 for Android 2.2.
If that does not solve the problem then try to add this tag to the AndroidManifest.xml of the application you want to test:
supports-screens android:anyDensity="true"
I'm having a problem with searchText() function used with Robotium.
I'm searching for this string:
<string name="provisioning_wizard_title_2">Service Activation Wizard (Step 2)</string>
I defined it in string xml, and I'm searching it this way:
Activity act=solo.getCurrentActivity();
String string = solo.getString(act.getResources().getIdentifier("provisioning_wizard_title_2", "string", act.getPackageName()));
It fails when I call
assertTrue(solo.searchText(string));
and it fails also if i call:
assertTrue(solo.searchText("Service Activation Wizard (Step 2)"));
even if that is the string I'm seeing in the focused screen activity.
The strange thing is that it works if I use the same string without last character:
assertTrue(solo.searchText("Service Activation Wizard (Step 2"));
and it works if I use
assertTrue(solo.searchText(string.substring(0, string.length()-1)));
I hope someone could help me.
Sorry for my english!
Thank you.
PROBLEM SOLVED
I solved problem thanks to Renas Reda (Founder and maintainer of Robotium).
The problem is due to regex support in searchText(); some characters will trigger it.
Use Pattern.quote(string)
Example: Pattern.qoute("provisioning_wizard_title_2") and eveything works good!
I usually use Solo#waitForText(...) so I make sure I'm not losing some race condition.
Try this:
assertTrue(solo.waitForText( solo.getString(R.string. provisioning_wizard_title_2) );
Make sure you are importing the correct R.java file from your production project (not the test project).
I'm not sure, what string you are fetching. Try to log it:
Log.d("Debug", "String is: " + string);
you should rather call, here may be another issue related to package as activity may be in another package than main package of application:
String string = solo.getString(act.getResources().getIdentifier(act.getPackageName()+":string/provisioning_wizard_title_2"), null, null));
You can also get all visible texts, to make sure, what is on the screen:
for(TextView textView : solo.getCurrentViews(TextView.class)) {
Log.d("DEBUG", "Text on the screen: " + textView.getText().toString());
}
I decided to learn a little bit android progrramming and by using Eclipse + android plug-in I made an android "hello world" application. After that I tryed something simple and used 3 TextViews:
ID:TextView1 String-Value:A=5
ID:TextView2 String-Value:B=10
ID:TextView3 String-Value:A+B=C=
I created them in main.xml menu by drag and drop so, to show them I'm using the flowing line:
setContentView(R.layout.main);
The Question is: I want to get string values of A and B and append the result of 5+10 to the string value of C actualy I did it but it take too much codes just for get str value of a TextView and editing it. Is there any short way like in .Net?
string Str = This.Label1.Text();
And is eclipse is the best IDE or am I should use something else?
When I have several textviews etc, Why am I should write unnecessary lines if there is a short way to do it.
Another Question is I get str-value of B which is 10 but when I tryed the same thing for A it says there is nothing inside VarAStr
*My Codes are:*
setContentView(R.layout.main);
TextView VarAStr = (TextView)findViewById(R.id.VarA);
TextView VarBStr = (TextView)findViewById(R.id.VarB);
TextView VarC = (TextView) findViewById(R.id.VarC);
try{
int VarA=Integer.parseInt(VarAStr.getText().toString().substring(2));
int VarB=Integer.parseInt(VarBStr.getText().toString().substring(2));
VarC.append(String.format("%d", (VarA+VarB)));
}
catch(Exception e){
Toast.makeText(this, e.toString() + " -" + VarAStr.getText().toString() + "-", Toast.LENGTH_LONG).show();//Show();
}
For the last question I found a bad code line in main.xml :) wich has no android:text="#string/VarA" I added this and problem solved.
Thank you for any respond.
You can always put the repeated code on a function that returns a string.
Something like:
public String getTextOfView(int id) {
TextView tmp = (TextView)findViewById(id);
return tmp.getText.toString();
}