while i have programming an android program about voice recognizing i have a little problem.what is the r.id cannot be resolved i have try to change my xml files which includes res folder.it doesnt works.what can i do ?
my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
Try cleaning your project (in Eclipse: Project->Clean...).
If that does not fix it delete your R.java file located under the gen folder. The R.java file should be regenerated for you.
I have done these thing to fix the problem you are describing, but your mileage may vary.
If somewhere in your class file you have a line like this:
TextView tv = (TextView) findViewById(R.id.tv1);
then R.id.tv1 must point to a TextView in your xml, which must first be given the id. So for example if you wanted to change the text in your TextView above you would have something like this to set the id in the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
//Set your id here:
android:id="#+id/TEXTVIEW1"
/>
and then to change the text, in your class you'd have this:
TextView tv = (TextView) findViewById(R.id.TEXTVIEW1);
tv.setText("How are you");
Insert this to your xml file's view tags.
android:id="#+id/YOUR_VIEW_ID"
Related
i would like to know why eclipse is showing warning "[I18N] Hardcoded string "TextView", should use #string resource" in the xml code below .Actually i am trying to get the text written by user in an edit Text in an activity to this current activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.01"
android:text="TextView" />
</LinearLayout>
The reason that you are receiving a warning, is due to the fact that you are trying to hardcode a string which is not good convention in Android programming due to possible redundancy:
<TextView
...
android:text="TextView" />
You should rather create a reference to a string in the .../res/values/strings.xml file like so:
<TextView
...
android:text="#string/TextView" />
.. and define it in your strings.xml file:
<string name="TextView">TextView</string>
Hope this helps.
As it says, you are using a "hard-coded" string which is less efficient than using the String resource. Simply remove
android:text="TextView"
if you don't want the warning to show. If you want it there then disregard the warning or add it to the String resource file. The Text property isn't needed. If you are expecting user input then you should change it to an EditText anyway unless you have a reason for using TextView
<EditText
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.01" />
Then if you want it to display something such as "Enter input here" in the View then you can add android:hint"Text to display". But this will give you the same warning if you don't add it to the strings.xml and use android:hint="#string/nameInStringsFile".
But these warnings are just that. Suggesting possibly more efficient methods or ways of implementing whatever you are doing.
Change the XML to the following to remove the warning
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.01" />
</LinearLayout>
The reason you are seeing the warning is because you had set the text to "TextView" inside the XML layout file. It is Android best practice to put all Strings inside the strings.xml file in your res/values folder which creates string resources. One you have a string in a resource file you can reference it from your layout file using the syntax "#string/string_name".
When creating a custom element with attributes in Android I need to put the namespace of the application in the layout.
For example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:whatever="http://schemas.android.com/apk/res/org.example.mypackage"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.example.mypackage.MyCustomView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>
Does this also requires that the structure of my project in Eclipse be the same as the name of the Android package as definied in the Manifest - as per the example?
Would this work too:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:whatever="http://schemas.android.com/apk/res/org.mycompany.myproduct"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.example.mypackage.MyCustomView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>
Its the package name of your application that should be reflected. So its correct to use it as you are doing.
For example:
In xmlns:whatever="http://schemas.android.com/apk/res/org.mycompany.myproduct the last part org.mycompany.myproduct should be the same as your package name. And you may change xmlns:whatever to anything like xmlns:theblitz but then make sure you do use theblitz as a prefix for your attributes in the xml.
For more info, read this
I have been following a book on Android "Beginning Android Application". I have been trying to figure out an application which shows images in a view. But unfortunately I could not run the application because of the following errors:
[2012-04-24 19:37:33 - Gallery] F:\eclipse workspace\Gallery\res\values
\attrs.xml:2:error: Found text "
[2012-04-24 19:37:33 - Gallery] ​​​​ " where item tag is expected
For convenience I am giving here the codes.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Bilai"
/>
<Gallery
android:id="#+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/image1"
android:layout_width="320px"
android:layout_height="250px"
android:scaleType="fitXY"
/>
</LinearLayout>
This application requires another xml file. It is named as attrs.xml under res/values.
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1" >
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
I think the problem lies within attrs.xml, but I dont know whats going on. And because of this the R.styleable also doesnt work either. Please help. Thanks in advance
I think you have some characters before each line in your attrs.xml due to a copy/past method.
Can you try to reformat your code by placing your text cursor at the beginning of each line and delete to return to previous line ?
caution [I18N] Hardcoded string"Would you like to give Zohan a call?", should use #string resource
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Would you like to give Zohan a call?"
android:layout_marginTop="200dp"
android:layout_marginLeft="20dp"
android:textSize="18dp"/>
You should not hard code your strings. Instead you should put your strings in values/strings.xml.
Ctrl+1 on the error it will show you the way to fix it.
Refer to the android string documentation: http://developer.android.com/guide/topics/resources/string-resource.html#String
You should store your texts in a resource properties file and link them to your XML file using the # notation.
See this link for more information.
I'm trying to set the font to a custom font. the font is in a subfolder of the assets folder called "fonts" . This is code in my onCreate() function. Eclipse suddendly brings up the debugger saying "source not found"
Typeface centuryGothic = Typeface.createFromAsset(this.getAssets(), "fonts/Century_Gothic_Bold.ttf");
TextView tv = (TextView) findViewById(R.id.TitleAct_title);
tv.setTypeface(centuryGothic);
setContentView(tv);
This is the xml in My main at the moment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="#string/TitleAct_title"
android:id="#+id/TitleAct_title"
android:textColor="#fff"
android:gravity="center_horizontal"
android:background="#347"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
I can't see anything wrong with this. Why is it bringing up the debugger and error "source not found"?
What you saw was the eclipse debugger looking for the source of the exception. next time, press continue and look ad the log of the exception in the Logcat window.
You should call setContentView(R.layout.main); before TextView tv = (TextView) findViewById(R.id.TitleAct_title);, and probably drop the call setContentView(tv);.