You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中SecondPage Activity启动失败问题求助

Android应用跳转SecondPage崩溃排查与修复

问题场景

作为Android开发新手,开发了一款点击按钮跳转至SecondPage的应用。MainActivity可正常启动无报错,但跳转至SecondPage时应用崩溃。

错误日志

Process: com.example.luckynumber, PID: 32726
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.luckynumber/com.example.luckynumber.SecondPage}: android.content.res.Resources$NotFoundException: String resource ID #0x5f
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5f
    at android.content.res.Resources.getText(Resources.java:444)
    at android.widget.TextView.setText(TextView.java:6412)
    at com.example.luckynumber.SecondPage.onCreate(SecondPage.java:42)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    ... 11 more

相关代码文件

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.LuckyNumber"
    tools:targetApi="31">
    <activity
        android:name=".SecondPage"
        android:exported="false" />
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

MainActivity.java

public class MainActivity extends AppCompatActivity {

    TextView displayText;
    EditText inputName;
    Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        displayText=findViewById(R.id.displayText);
        inputName=findViewById(R.id.inputName);
        btn1=findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToSecondPage();
            }
        });

    }
    public void goToSecondPage(){
        String userName=inputName.getText().toString();
        Intent intent=new Intent(getApplicationContext(),
                SecondPage.class);
        intent.putExtra("name",userName);
        startActivity(intent);

    }

}

SecondPage.java

public class SecondPage extends AppCompatActivity {
    TextView displayLucky, luckyNumber;
    Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_second_page);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        displayLucky=findViewById(R.id.displayLucky);
        luckyNumber=findViewById(R.id.luckyNumber);
        btn2=findViewById(R.id.btn2);

        Intent intent=getIntent();
        String userName=intent.getStringExtra("name");

        int randomNumber=generateRandomNumber();

        luckyNumber.setText(randomNumber);

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareData(userName,randomNumber);

            }
        });


    }

    public int generateRandomNumber(){
        Random random=new Random();
        int upper_limit=100;

        return random.nextInt(upper_limit);
    }

    public void shareData(String username,int num){
        Intent i=new Intent(Intent.ACTION_SEND);
        i.setType("text/plain");

        i.putExtra(Intent.EXTRA_SUBJECT,username+" got lucky today");
        i.putExtra(Intent.EXTRA_TEXT,"His lucky number is "+num);

        startActivity(Intent.createChooser(i,"Choose a platform"));

    }
}

问题原因与修复方案

  1. 核心崩溃原因:
    在SecondPage的onCreate方法中,luckyNumber.setText(randomNumber);调用了TextView的setText(int)方法。该方法会将传入的整数解析为字符串资源ID,而非直接显示数字。当随机生成的数字(如95,对应十六进制0x5f)不存在对应的字符串资源时,就会抛出Resources$NotFoundException。

  2. 修复代码:
    将整数转换为字符串后再设置给TextView,修改该行代码为:

    luckyNumber.setText(String.valueOf(randomNumber));
    // 或使用字符串拼接方式:luckyNumber.setText("" + randomNumber);
    
  3. 额外注意:
    确保SecondPage类中已正确声明成员变量TextView displayLucky, luckyNumber; Button btn2;(原代码中缺失,需补充)。

内容的提问来源于stack exchange,提问作者Mridul Hemrajani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 13:29:50