gpt4 book ai didi

java - 切换 Activity 时崩溃

转载 作者:可可西里 更新时间:2023-11-01 10:33:05 28 4
gpt4 key购买 nike

<分区>

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

list :

    <?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>

周期表屏幕:

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

创建按钮.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);

}
});
}
}

特定元素:

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

这是我收到的错误:

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.

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com