Reputation: 344
I am about to create my first android app :This is a screenshot of how the app looks
So The main idea of this app is to generate random numbers in the two buttons and the user guess which number is bigger if he guessed it right:
This is the XML layout
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/app_name"
android:gravity="center_horizontal"
android:text="Press the button of the larger Number.if you get it right you will earn point !if you get it wrong you'll lose a point"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/left_button_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textStyle="bold" />
<Button
android:id="@+id/right_button_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="0"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="top"
android:layout_marginBottom="57dp"
android:gravity="center_horizontal"
android:text="points : "
android:textStyle="italic" />
<TextView
android:id="@+id/score_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:text="0" />
<Button
android:id="@+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:text="Reset" />
This is the Java Code
public class MainActivity extends AppCompatActivity {
int leftNumber;
int rightNumber;
private OnClickListener ButtonClickListener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.left_button_number:
if (rightNumber < leftNumber)
updateTheScore(1);
else
updateTheScore(-1);
break;
case R.id.right_button_number:
if (rightNumber > leftNumber)
updateTheScore(1);
else
updateTheScore(-1);
break;
case R.id.reset_button:
resetTheScore();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
numberGenerator();
Button rightButton = (Button) findViewById(R.id.right_button_number);
Button leftButton = (Button) findViewById(R.id.left_button_number);
Button resetButton = (Button) findViewById(R.id.reset_button);
rightButton.setOnClickListener(ButtonClickListener);
leftButton.setOnClickListener(ButtonClickListener);
resetButton.setOnClickListener(ButtonClickListener);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void updateTheScore(int n) {
TextView scoreView = (TextView) findViewById(R.id.score_text_view);
int score = Integer.parseInt(scoreView.getText().toString());
score += n;
scoreView.setText(score);
numberGenerator();
}
public void resetTheScore() {
TextView scoreView = (TextView) findViewById(R.id.score_text_view);
scoreView.setText(0);
numberGenerator();
}
public void numberGenerator() {
int min = 1;
int max = 100;
Random r = new Random();
leftNumber = r.nextInt(max - min + 1) + min;
rightNumber = r.nextInt(max - min + 1) + min;
displayMessageOnButton(leftNumber, rightNumber);
}
public void displayMessageOnButton(int left, int right) {
Button rightButton = (Button) findViewById(R.id.right_button_number);
Button leftButton = (Button) findViewById(R.id.left_button_number);
rightButton.setText(right);
leftButton.setText(left);
}
This is the logcat
String resource ID #0x4a
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x4a
at android.content.res.Resources.getText(Resources.java:266)
at android.widget.TextView.setText(TextView.java:3627)
at com.example.android.big.MainActivity.displayMessageOnButton(MainActivity.java:87)
at com.example.android.big.MainActivity.numberGenerator(MainActivity.java:81)
at com.example.android.big.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:4531)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 53
Reputation: 1039
public void numberGenerator() {
int min = 1;
int max = 100;
Random r = new Random();
leftNumber = r.nextInt((max - min) + 1) + min;
rightNumber = r.nextInt((max - min) + 1) + min;
displayMessageOnButton(leftNumber, rightNumber);
}
Can not set Integer to text
public void displayMessageOnButton(int left, int right) {
Button rightButton = (Button) findViewById(R.id.right_button_number);
Button leftButton = (Button) findViewById(R.id.left_button_number);
rightButton.setText(right+"");
leftButton.setText(left+"");
}
Upvotes: 4
Reputation: 2916
public void updateTheScore(int n) {
TextView scoreView = (TextView) findViewById(R.id.score_text_view);
int score = Integer.parseInt(scoreView.getText().toString());
score += n;
scoreView.setText(score); // <---- this isn't working
numberGenerator();
}
You passed an int as a parameter to your scroteView.setText(); . In this case, int is interpreted as a ressource ID (which is obvously not what you wanted). A simple
scoreView.setText(score + "");
(implicit String cast) will fix it.
Upvotes: 4