I need assistence with android app life cycle - android

I've created this simple example to illustrate what i am trying to accomplish.
This is my first layout:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class LifeCycleActivity extends Activity implements OnCheckedChangeListener {
/** Called when the activity is first created. */
private RadioButton rbR, rbG, rbB;
private RadioGroup rg;
private Button next;
int color=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rbR = (RadioButton) findViewById(R.id.radio0);
rbB = (RadioButton) findViewById(R.id.radio1);
rbG = (RadioButton) findViewById(R.id.radio2);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
next = (Button) findViewById(R.id.button1);
final Intent it = new Intent(this, next.class);
final Bundle b = new Bundle();
rg.setOnCheckedChangeListener(this);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
b.putInt("color", color);
it.putExtras(b);
startActivity(it);
}
});
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId==rbR.getId()) color=1;
if (checkedId==rbB.getId()) color=2;
if (checkedId==rbG.getId()) color=3;
}
}
This is the second layout:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
public class next extends Activity {
private LinearLayout ll;
private ImageView im;
private TextView tv;
private Button save;
private Bundle extras;
private int color=0;
private String selColor="";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
ll = (LinearLayout) findViewById(R.id.mainll);
ll.setOrientation(LinearLayout.VERTICAL);
im = new ImageView(this);
tv = new TextView(this);
save = new Button(this);
save.setText("save");
extras = getIntent().getExtras();
color = extras.getInt("color");
im.setImageResource(R.drawable.ic_launcher);
ll.addView(im);
if (color == 1) selColor = "RED";
if (color == 2) selColor = "BLUE";
if (color == 3) selColor = "GREEN";
tv.setText(selColor);
tv.setGravity(Gravity.CENTER);
ll.addView(tv);
ll.addView(save);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// here i want to save and exit
// so i can call onPause(), then finish()
// do not know how exactly since i have to follow some goals
// that i need for this example
}
});
}
}
I also have 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" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="red" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blue" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="green" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
AND next.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
As you can see, based on color selection in first layout I dynamically build the second layout.
This is what I want to do, help me please (add code if possible):
on the second layout if I press save, I want to save app state (whatever I built on that page) and exit.
If I add one more button to create other dynamic objects on that layout (EditText with name, or another layout with different objects) than if user saves, he/she will also save new objects (This part can be done later)
if user saved that page once then i want him to be able to get the same app state that he/she was in before exiting (of course if he/she selects the same radio button).
If he/she selects another button, i do not need to show that previous state, since the selection is different.

The trick with sharedpreferences did for me. I was finally able to same and retrieve my data onPause and onStart. Even though this worked fine, I appreciate the answer of Kevin_Dingo which is defiantly more professional way to go.

Related

How to create dynamic controls and store the values in sqlite?

Iam doing an android application. In that i want to create dynamic controls(edittext) like android core contacts application. After entering data in the ediitext i want to save the data to sqlite database by clicking a button named save. As Iam new to android i dont have any idea to create dynamic controls and storing its row values. Please help me if anybody knows.
My code:
package com.xiaochaoyang.dynamicviews;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
// Parent view for all rows and the add button.
private LinearLayout mContainerView;
// The "Add new" button
private Button mAddButton;
// There always should be only one empty row, other empty rows will
// be removed.
private View mExclusiveEmptyView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_container);
mContainerView = (LinearLayout) findViewById(R.id.parentView);
mAddButton = (Button) findViewById(R.id.btnAddNewItem);
// Add some examples
inflateEditRow("Xiaochao");
inflateEditRow("Yang");
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// TODO: Handle screen rotation:
// encapsulate information in a parcelable object, and save it
// into the state bundle.
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// onClick handler for the "Add new" button;
public void onAddNewClicked(View v) {
// Inflate a new row and hide the button self.
inflateEditRow(null);
//v.setVisibility(View.GONE);
}
// onClick handler for the "X" button of each row
public void onDeleteClicked(View v) {
// remove the row by calling the getParent on button
mContainerView.removeView((View) v.getParent());
}
// Helper for inflating a row
private void inflateEditRow(String name) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.row, null);
final ImageButton deleteButton = (ImageButton) rowView
.findViewById(R.id.buttonDelete);
final EditText editText = (EditText) rowView
.findViewById(R.id.editText);
if (name != null && !name.isEmpty()) {
editText.setText(name);
} else {
mExclusiveEmptyView = rowView;
//deleteButton.setVisibility(View.INVISIBLE);
}
// A TextWatcher to control the visibility of the "Add new" button and
// handle the exclusive empty view.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
//mAddButton.setVisibility(View.GONE);
//deleteButton.setVisibility(View.INVISIBLE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView) {
mContainerView.removeView(mExclusiveEmptyView);
}
mExclusiveEmptyView = rowView;
} else {
if (mExclusiveEmptyView == rowView) {
mExclusiveEmptyView = null;
}
mAddButton.setVisibility(View.VISIBLE);
deleteButton.setVisibility(View.VISIBLE);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
// Inflate at the end of all rows but before the "Add new" button
mContainerView.addView(rowView, mContainerView.getChildCount() );
}
}
My layouts:
Row Cointainer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical" >
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnAddNewItem"
android:layout_width="21dp"
android:layout_height="wrap_content"
android:background="#drawable/transparent_background"
android:gravity="center_vertical"
android:onClick="onAddNewClicked"
android:layout_gravity="right"
android:paddingLeft="5dp"
android:text="+"
android:textColor="#android:color/black" />
</ScrollView>
</LinearLayout>
Row.xml
<?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="50dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:singleLine="true"
android:ems="10"
android:hint="" >
<requestFocus />
</EditText>
<Spinner
android:id="#+id/spinnerCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:entries="#array/categories"
android:gravity="right" />
<ImageButton
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/content_remove"
android:background="#drawable/transparent_background"
android:contentDescription="#string/button_delete_row_description"
android:onClick="onDeleteClicked"/>
</LinearLayout>
First of all, while posting your question in SO, please show what have you tried so far, post your logcat if your app is getting force closed.
To work with controls, please refer to http://developer.android.com/reference/packages.html
There you find the methods available to work with controls.
Check this link: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
It helps to learn and perform CRUD operations on SQLite Database.
At last, what you left with is, get data (or input) from controls, and insert into database.
Also, please go for googling.

How to change textView to a #string and raising

so what i am tring to do is, i am having a book like-app, and want to setup a button when click it change the textview to a string in the #string rec. but in a different way
i will put the code just a moment, lets say i have a string in #string that is NH1,NH2,NH3
i can do for button View.setText(R.string.NH1); that will work but i want to create a counter and make it so every time clicked counter++ and want it to be like this View.setText(R.string.NH+counter); if you know what i mean so each time it clicked to goes to next #string without changing layout every time and without an array because the paragraph are very long
also if i have NH1-10 and the counter did not stop and still goes on what will i do.
if you are sill here thank you for reading.
XML(sheetpaper)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="496dp"
android:background="#drawable/bg1"
android:gravity="top"
android:orientation="vertical"
android:weightSum="100" >
<Button
android:id="#+id/bPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="51dp"
android:background="#android:color/transparent"
android:text=" " />
<TextView
android:id="#+id/tvHadith"
android:layout_width="310dp"
android:layout_height="210dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:text="#string/NH1"
android:textColor="#android:color/white" />
<Button
android:id="#+id/bNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/bPrev"
android:layout_alignBottom="#+id/bPrev"
android:layout_alignRight="#+id/tvHadith"
android:background="#android:color/transparent"
android:gravity="center"
android:text=" " />
"
</RelativeLayout>
</LinearLayout>
Java (Nawawi)
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Nawawi extends Activity {
private static final String NH = null;
Button Next;
Button Prev;
TextView View;
int counter = 0;
String H=NH;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sheetpaper);
Next = (Button) findViewById(R.id.bNext);
Prev = (Button) findViewById(R.id.bPrev);
View = (TextView) findViewById(R.id.tvHadith);
Next.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
View.setText(R.string.H);
}
}); Prev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
View.setText(R.string.);
}
});
}
}
You could do this by creating an XML string array, so your strings will still be referenced in resources, but you'll be able to loop through them.
Something like this, in values/strings.xml:
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
And then in Activity:
Resources res = getResources();
final String[] planets = res.getStringArray(R.array.planets_array);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter < planets.length) {
textView.setText(planets[counter]);
counter++;
}
}
});
Be sure to check the counter against arrays length, otherwise you will get an IndexOutOfBoundsException.

Android: Simpify many classes and xml

Edit: I cannot get this to work correctly. Probably because I have no idea what I am doing. Anyways, here's my code. If anyone could help, I'd be very grateful: I needs to get it to display the battery mood image for the corresponding mod...
Themes:
package com.cydeon.plasmamodz;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Themes extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
ActionBar actionBar = getActionBar();
actionBar.hide();
Button Plus = (Button) findViewById(R.id.button1);
Button Blue = (Button) findViewById(R.id.button2);
Plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(Themes.this, Bmod.class);
i.putExtra("drawableResource", R.drawable.blue);
Themes.this.startActivity(i);
}
});
Blue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent a = new Intent(Themes.this, Bmod.class);
a.putExtra("drawableResource1", R.drawable.plus);
Themes.this.startActivity(a);
}
});
}
}
Bmods:
package com.cydeon.plasmamodz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class Bmod extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery);
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.blue);
ImageView img = (ImageView) findViewById(R.id.iv1);
img.setImageResource(R.drawable.blue);
Intent a = getIntent();
int drawableResource1 = a.getIntExtra("drawableResource1", R.drawable.plus);
ImageView img1 = (ImageView) findViewById(R.id.iv1);
img1.setImageResource(R.drawable.plus);
}
}
battery(xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bInstall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Install" />
<Button
android:id="#+id/bReturn"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Return" />
<ImageView
android:id="#+id/iv1"
android:layout_width="match_parent"
android:layout_height="800dp" />
</RelativeLayout>
Intent i = new Intent(this, BaseClassForMod);
i.putExtra("drawableResource", R.drawable.this_mod_drawable);
startActivity(i);
Then in that Activity's onCreate():
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.default);
//Get a reference to your ImageView...
imageView.setImageResource(drawableResource);
Don't trust this code to compile, but that's the general idea. Use the same Activity for all of them, pass along the proper resource ID in the intent (e.g. for mod1, send the drawable ID for mod1), then in the activity, check for that resource ID and set it programmatically.
You shouldn't need 50 classes and 50 xml layouts if every one of them does the same thing. Make one activity and one layout. When the user selects something, pass an id of some kind as an Intent extra to the second activity so it can load whatever item is appropriate. I don't know how your data is modeled, but there should be a way to uniquely identify each option (and if there isn't, you should implement one).
Your first activity also doesn't need a button for each item. Use a ListView and an Adapter, and then you just need to provide a layout for one row.
make a single activity only. Inside of it get a reference to your image:
ImageView iv = (ImageView)findViewById(R.id.yourImgId);
Then set the picture to whichever one you want like:
iv.setImageResource(R.drawable.battery_img_1);

Bring a view on selecting one view

hii i want to create a ui in which as i select a radio button there should come a textview.
when that button is not selected text view should not be visible. and as button got selected it should be visible..can i implement this??
Inside the listener where you check if the radio button is selected or not:
findViewById(R.id.yourtextview).setVisibility(View.INVISIBLE);
and
findViewById(R.id.yourtextview).setVisibility(View.VISIBLE);
You can choose between INVISIBLE and GONE.
Tutorial to manage radio buttons
Here is sample codes for you...
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
private RadioButton radioButton1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioButton1 = (RadioButton) findViewById(R.id.RadioButton1);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(radioButton1.isChecked()) {
findViewById(R.id.textView).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.textView).setVisibility(View.GONE);
}
}
});
}
}
And here is the xml layout: Main.xml
Hope it will help you a lot...
public class _alefon_radio extends Activity implements OnCheckedChangeListener {
/** Called when the activity is first created. */
private TextView tx;
private RadioGroup rg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tx = (TextView) findViewById(R.id.tvv);
rg = (RadioGroup) findViewById(R.id.rgroup);
rg.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
// for R.id.option1
case R.id.option1:
tx.setText("option one is checked");
//tx.setVisibility(0); //visible
break;
default:
tx.setText("");
//tx.setVisibility(4); //invisible
}
}
}
and layout:
<?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:id="#+id/tvv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<RadioGroup
android:id="#+id/rgroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:id="#+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>
</LinearLayout>
I used edit text instead of setting visbile/invisible but also I included visibilty controls (commented) if you`d like to use this way.
I hope this is what you are looking for.
good luck,

Resource not found TextView

I am taking my first steps in Android and am starting with a very simple app which keeps track of progress through a knitting pattern and shows the instructions for the relevant row.
I want to update a couple of TextView objects programmatically. However, using getViewById() does not seem to identify them properly and the app crashes.
Having searched on Google it seems there are sometimes problems with the XML namespace in the layout XML but mine looks OK. Is it something to do with scope, perhaps?
instructions.java (this is the only activity)
package uk.co.oketchup.blanketsquare;
import android.app.Activity;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.view.View;
public class instructions extends Activity
{
private int mRow;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* retrieve row from shared preferences, or start from zero if nothing there */
SharedPreferences settings = getPreferences(MODE_PRIVATE);
mRow = settings.getInt("row",0);
setContentView(R.layout.main);
/* associate onClick listeners with the two buttons */
final Button btnIncrement = (Button) findViewById(R.id.increment);
btnIncrement.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Increment row
++mRow;
calcAndUpdate();
}
});
final Button btnDecrement = (Button) findViewById(R.id.decrement);
btnDecrement.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Decrement row
--mRow;
calcAndUpdate();
}
});
}
private void calcAndUpdate() {
String Instructions;
int knit1;
int knit2;
int remaining;
if (mRow%2 == 1 )
{
/* Odd row */
knit1 = 40 - mRow;
Instructions = "K" + knit1;
remaining = knit1;
}
else
{
/* Even row */
knit1 = 18 - mRow/2;
knit2 = knit1 + 1;
Instructions = "Sl 1, k" + knit1 + ", [sl 1 kwise] 2 times, k1, p2sso, k" + knit2;
remaining = knit1 + knit2 + 2;
}
/* Update the display */
TextView tRow = (TextView) findViewById(R.id.row);
TextView tInstr = (TextView) findViewById(R.id.instr);
TextView tStRem = (TextView) findViewById(R.id.stitchremain);
/* Set the text */
tRow.setText(mRow);
tInstr.setText(Instructions);
tStRem.setText(remaining);
}
}
/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Row"
/>
<TextView
android:id="#+id/instr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Instructions"
android:layout_below="#id/row"
/>
<Button
android:id="#+id/increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_alignParentBottom="true" />
<Button
android:id="#+id/decrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_toRightOf="#id/increment"
android:layout_alignParentBottom="true" />
<TextView
android:id="#+id/stitchremain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stitches remaining"
android:layout_above="#id/increment"
/>
</RelativeLayout>
The TextView objects seem to have been registered ok as they appear in R.java
public static final class id {
public static final int decrement=0x7f050003;
public static final int increment=0x7f050002;
public static final int instr=0x7f050001;
public static final int row=0x7f050000;
public static final int stitchremain=0x7f050004;
}
Here is the error message shown in ddms.
Uncaught handler: thread main exiting due to uncaught exception
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:200)
at android.widget.TextView.setText(TextView.java:2813)
at uk.co.oketchup.blanketsquare.instructions.calcAndUpdate(instructions.java:75)
at uk.co.oketchup.blanketsquare.instructions.access$100(instructions.java:11)
at uk.co.oketchup.blanketsquare.instructions$1.onClick(instructions.java:33)
[etc]
Many thanks for your help.
mRow is an integer. When you call setText(mRow) on line 75, it thinks that you are trying to set the text with a String resource with ID = the value of mRow.
Instead, do:
tRow.setText(Integer.toString(mRow));
You should always convert other values to string before setting it to textview, like
txtX.setText(Integer.toString(intVal));

Categories

Resources