2

When I run this app using Android Studio, it is crashing while switching from PeriodicTableScreen, via the onClick created in createButtons.java.

Manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ros_dhhiggins.example.com.periodictable">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/Home">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity
            android:name=".PeriodicTableScreen">
            <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".SpecificElement">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>  

PeriodicTableScreen:

package ros_dhhiggins.example.com.periodictable;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import static java.security.AccessController.getContext;
public class PeriodicTableScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_periodic_table_screen);
    GridLayout grid = (GridLayout) findViewById(R.id.layoutGrid);
    createButtons newButtons = new createButtons(this);
    ImageButton[] imageButtons;
    imageButtons = newButtons.build();
    for(int i = 1; i<=2; i++){
        grid.addView(imageButtons[i]);
    }
}
}

CreateButtons.java:

package ros_dhhiggins.example.com.periodictable;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import java.lang.reflect.Array;
import static android.R.attr.targetActivity;
import static android.R.attr.type;
public class createButtons extends Activity{
private Context context;
createButtons(Context context){
this.context = context;
    }


public ImageButton[] build(){
        ImageButton[] elementButtons = new ImageButton[126]; // need 126 images or crash
            for(int i=1; i<=2; i++){

                String name; //the name of the images
                name = "image"+i;
                    elementButtons[i] = new ImageButton(context);
                    elementButtons[i].setImageResource(getImage(context, name));
                    elementButtons[i].setBackgroundResource(0);
                    setButtonClick(i,elementButtons[i]);
                    // creates the imageButton and sets it with the image specified by name
                }
                                //create buttons with onclick that takes the button number
                                //sets button number as extra
                                //starts new activity with that extra and use String Array
                                //use extra to find the info we need about the element and display

            return elementButtons;
    }


                private static int getImage(Context context, String name) {
                    return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
}

                private void setButtonClick(final int i,ImageButton buttonToSet){
                    buttonToSet.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                        Intent specificElement = new Intent(context, SpecificElement.class);
                        specificElement.putExtra("elementNumber", i);
                        context.startActivity(specificElement);

                        }
                    });
                }
}

SpecificElement:

package ros_dhhiggins.example.com.periodictable;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SpecificElement extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_specific_element);
    Intent intent = getIntent();
    int arrayNumber = intent.getIntExtra("elementNumber", 0);
    TextView text = (TextView) findViewById(R.id.elementInfo);
    text.setText(arrayNumber);
}
}

This Was the Error I Recieved:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ros_dhhiggins.example.com.periodictable/ros_dhhiggins.example.com.periodictable.SpecificElement}: android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:299)
at android.widget.TextView.setText(TextView.java:4132) 
at ros_dhhiggins.example.com.periodictable.SpecificElement.onCreate(SpecificElement.java:18)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Application terminated.
Cœur
  • 37,241
  • 25
  • 195
  • 267
Danny
  • 37
  • 5
  • 1
    Does this help: http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this ? – GhostCat Mar 24 '17 at 16:12
  • 1
    someone already answered but i'll definitely use that in the future. Thanks! – Danny Mar 24 '17 at 16:18
  • Besides, the real take away here: **search** the net for your exception messages. When you go look for `Caused by: android.content.res.Resources$NotFoundException: String resource ID` you will find several answers to this problem ;-) ... and if I wouldnt have been awake since 4 am; I might have noticed earlier and could have closed out that DUP 19.5 minutes ago ;-) – GhostCat Mar 24 '17 at 16:23
  • But the good thing: nice question for a newbie! – GhostCat Mar 24 '17 at 16:24

1 Answers1

1

In SpecificElement

change

text.setText(arrayNumber);

to

text.setText(arrayNumber+"");

or

text.setText(String.valueOf(arrayNumber));
John Joe
  • 12,412
  • 16
  • 70
  • 135