切换活动时崩溃

时间:2017-03-24 16:03:10

标签: java android windows

当我使用Android Studio运行此应用程序时,从PeriodicTableScreen切换到通过createButtons.java中创建的onClick时崩溃。

清单:

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]);
    }
}
}

PeriodicTableScreen:

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);

                        }
                    });
                }
}

CreateButtons.java:

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);
}
}

SpecificElement:

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.

这是我收到的错误:

dataLayer = [{
  key: 'value',
  key2: {
    key3: ['value2','value3','value4'],
    key4: ['value5','value6']
  }
}];

1 个答案:

答案 0 :(得分:1)

SpecificElement

更改

text.setText(arrayNumber);

text.setText(arrayNumber+"");

text.setText(String.valueOf(arrayNumber));