java - Getting error while changing to string-array from string in text field of activity xml file in android studio -
i want display random strings each time android app opened. using stackoverflow, able write java program getting random strings resource file.
package com.example.krishna.quotes; import android.support.v7.app.appcompatactivity; import android.os.bundle; import java.util.random; import android.content.res.resources; import android.widget.textview; public class mainactivity extends appcompatactivity { private string[] mystring; private static final random rgenerator = new random(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); resources res = getresources(); mystring = res.getstringarray(r.array.quotes_array); string q = mystring[rgenerator.nextint(mystring.length)]; textview tv = (textview) findviewbyid(r.id.text); tv.settext(q); } }
when used string type in text field of activity xml file, app built without errors.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.krishna.quotes.mainactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string-array/quotes_array" android:textalignment="center" android:textsize="23sp" android:gravity="center|center_horizontal" android:layout_centervertical="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> </relativelayout>
but since don't want 1 string displayed everytime opened app, collected strings string-array.
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="quotes_array"> <item>@string/quote1</item> <item>@string/quote2</item> <item>@string/quote3</item> <item>@string/quote4</item> <item>@string/quote5</item> <item>@string/quote6</item> <item>@string/quote7</item> <item>@string/quote8</item> <item>@string/quote9</item> <item>@string/quote10</item> <item>@string/quote11</item> <item>@string/quote12</item> <item>@string/quote13</item> <item>@string/quote14</item> <item>@string/quote15</item> </string-array> </resources>
changing text field in activity xml file gave following error:
error:(15, 23) no resource found matches given name (at 'text' value '@string-array/quotes_array'). error:execution failed task ':app:processdebugresources'.
com.android.ide.common.process.processexception: org.gradle.process.internal.execexception: process 'command '/home/krishna/android/sdk/build-tools/23.0.2/aapt'' finished non-zero exit value 1
the problem string-array type probably, android studio not able handle. how overcome problem build first android app? i'm quite excited show off app friends, small error coming in way. thank help. please don't call me noob
instead of writing
string-array name = "quotes_array"
in strings.xml
use
array name="quotes_array"
Comments
Post a Comment