0

I'm new to android development. I'm just trying to make a simple guessing type app. But when I deploy to my phone it instantly crashes.

Here's the xml file

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="5"
    android:orientation="horizontal"
    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.association.researchminigame.MainActivity" >

    <Space
        android:id="@+id/Space1"
        android:layout_column="1"
        android:layout_gravity="fill_vertical"
        android:layout_row="5" />

    <EditText
        android:id="@+id/editTextIngredients"
        android:layout_width="116dp"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="left|top"
        android:layout_row="2"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="113dp"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="left"
        android:layout_marginTop="28dp"
        android:layout_row="1"
        android:text="How many ingredients(1-3)?" />

    <TextView
        android:id="@+id/textView2"
        android:layout_column="4"
        android:layout_gravity="left"
        android:text="How many testers?" />

    <Button
        android:id="@+id/ButtonReset"
        style="?android:attr/buttonStyleSmall"
        android:layout_column="3"
        android:layout_gravity="left"
        android:layout_marginBottom="65dp"
        android:layout_row="7"
        android:text="Reset" />

    <Button
        android:id="@+id/buttonGuess"
        style="?android:attr/buttonStyleSmall"
        android:layout_column="3"
        android:layout_gravity="left"
        android:layout_row="6"
        android:text="Guess" />

    <NumberPicker
        android:id="@+id/numberPicker1"
        android:layout_column="2"
        android:layout_columnSpan="2"
        android:layout_gravity="left"
        android:layout_row="5" />

    <EditText
        android:id="@+id/editTextTesters"
        android:layout_width="114dp"
        android:layout_gravity="fill_horizontal"
        android:layout_row="2"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView3"
        android:layout_column="2"
        android:layout_columnSpan="3"
        android:layout_gravity="left"
        android:layout_marginTop="33dp"
        android:layout_row="3"
        android:text="Current testers:" />

    <TextView
        android:id="@+id/textViewTesters"
        android:layout_column="3"
        android:layout_gravity="left"
        android:layout_marginTop="14dp"
        android:layout_row="4"
        android:text="TextView" />

    <TextView
        android:id="@+id/textViewStatus"
        android:layout_column="1"
        android:layout_columnSpan="4"
        android:layout_gravity="left"
        android:layout_marginBottom="31dp"
        android:layout_row="8"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</GridLayout>

Here's my code:

package com.association.researchminigame;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;


public class MainActivity extends Activity {

    NumberPicker numPick;
    Button guess;
    Button reset;
    Random RNG = new Random();
    EditText editIngredients;
    EditText editTesters;
    int testers;
    int ingredients;
    int target;
    TextView viewTesters;
    TextView status;
    int numGuessed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        numPick = (NumberPicker)findViewById(R.id.numberPicker1);
        guess = (Button)findViewById(R.id.buttonGuess);
        reset = (Button)findViewById(R.id.ButtonReset);
        editIngredients = (EditText)findViewById(R.id.editTextIngredients);
        editTesters = (EditText)findViewById(R.id.editTextTesters);
        numPick.setMaxValue(10);
        numPick.setMinValue(0);
        viewTesters = (TextView)findViewById(R.id.textViewTesters);
        viewTesters.setText(10);
        guess.setVisibility(View.GONE);
        status = (TextView)findViewById(R.id.textViewStatus);
        status.setText("");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onClick(View v){
        if (v == guess){
            guessCheck();
        }
        if (v == reset){
            testers = Integer.parseInt(editTesters.getText().toString());
            viewTesters.setText(testers);
            ingredients = Integer.parseInt(editIngredients.getText().toString());
            target = RNG.nextInt(10) + 1;
            guess.setVisibility(View.VISIBLE);
        }
    }

    public void guessCheck(){
        numGuessed = numPick.getValue();
        if (numGuessed == target){
            status.setText("You win! Feel free to play again.");
        }
        else{
            if ((testers-numGuessed) > 0){
                testers -= numGuessed;
                viewTesters.setText(testers);
            }
            else{
                status.setText("You lose! Play again!");
                guess.setVisibility(View.VISIBLE);
            }
        }
    }
}

`

Got a new set of errors:

09-22 21:49:35.857: E/ACDB-LOADER(188): Error: ACDB AudProc vol returned = -19
09-22 21:49:38.577: E/AndroidRuntime(14583): FATAL EXCEPTION: main
09-22 21:49:38.577: E/AndroidRuntime(14583): Process: com.association.researchminigame, PID: 14583
09-22 21:49:38.577: E/AndroidRuntime(14583): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.association.researchminigame/com.association.researchminigame.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0xa
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.os.Looper.loop(Looper.java:136)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.app.ActivityThread.main(ActivityThread.java:5001)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at java.lang.reflect.Method.invokeNative(Native Method)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at java.lang.reflect.Method.invoke(Method.java:515)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at dalvik.system.NativeStart.main(Native Method)
09-22 21:49:38.577: E/AndroidRuntime(14583): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0xa
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.content.res.Resources.getText(Resources.java:244)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.widget.TextView.setText(TextView.java:3888)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at com.association.researchminigame.MainActivity.onCreate(MainActivity.java:43)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.app.Activity.performCreate(Activity.java:5231)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-22 21:49:38.577: E/AndroidRuntime(14583):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-22 21:49:38.577: E/AndroidRuntime(14583):    ... 11 more
09-22 21:49:48.377: E/WindowManager(767): Starting window AppWindowToken{43862890 token=Token{42f482b8 ActivityRecord{432f71a8 u0 com.association.researchminigame/.MainActivity t27}}} timed out

Here is my strings.xml by the way:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ResearchMinigame</string>
    <string name="action_settings">Settings</string>
    <string name="ingredients">How many ingredients(1 to 3)?</string>
    <string name="testers">How many testers?</string>
    <string name="reset">Reset</string>
    <string name="guess">Guess</string>
    <string name="current">Current testers:</string>
    <string name="blank"> </string>

</resources>

I don't even know what to search for. I probably forgot some vital step in creating some of the code. Any help would be very much appreciated! I tried doing some research... But it's kind of hard to research crashes that you have no clue about!

Raistlin
  • 167
  • 8
  • Your issue is in line number 35. Make sure that button doesn't overlap the numberpicker in ur xml file. Please post ur full xml file. – Kosh Sep 23 '14 at 00:59
  • Some of the xml wasn't displaying for some reason, my bad. But how is the button and the numberpicker overlapping? There is the /> before the start of the numberpicker – Raistlin Sep 23 '14 at 01:23
  • It has nothing to do with overlapping. – Kevin Krumwiede Sep 23 '14 at 01:25
  • @Raistlin sorry my bad, never saw that you using Gridlayout. – Kosh Sep 23 '14 at 01:26
  • If you don't know what to search for, please read [this page](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – yildirimyigit Sep 23 '14 at 01:30
  • Thanks! I read that... Found the stack trace as not being able to cast a button as a number picker... But I can't find any instance that I did that! – Raistlin Sep 23 '14 at 01:46
  • It's now giving me a new set of errors. But I have no idea what that stack trace means. – Raistlin Sep 23 '14 at 01:52
  • Don't change what you posted. Now nobody else will be able to benefit from the original question and solution. – Kevin Krumwiede Sep 23 '14 at 02:08
  • Oops. Sorry! I guess I should have just made a new section. – Raistlin Sep 23 '14 at 02:13

3 Answers3

0

Is this line 35?

    numPick = (NumberPicker)findViewById(R.id.numberPicker1);

If so, the error indicates that you have defined numberPicker1 as a Button instead of a NumberPicker in your XML. But the XML you posted is correct, so perhaps you already fixed it but you need to clean and rebuild.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
0

in your codes

viewTesters.setText(10);

You should write this

viewTesters.setText(Integer.toString(10));

TextView.setText is an overloaded function, int varibales are for resources id.

楊惟鈞
  • 622
  • 6
  • 12
0
 viewTesters = (TextView)findViewById(R.id.textViewTesters);
 viewTesters.setText(10);  // BUG HERE
 guess.setVisibility(View.GONE);
 status = (TextView)findViewById(R.id.textViewStatus);
 status.setText("");

the setText method of TextView both accept CharSequence and a resourceId(you can define it in you res/strings.xml), you called viewTesters.setText(10), the value 10 will be regarded as a resource id, and android intent to find a String by this id. In the exception stack, it shows:

android.content.res.Resources$NotFoundException: String resource ID #0xa

0xa is 10, so...

if you just want to set the TextView's value to 10, you can call setText(""+10);

enter image description here

EricHua23
  • 172
  • 9