代码之家  ›  专栏  ›  技术社区  ›  Mridul Hemrajani

无法在Android Studio中启动活动

  •  0
  • Mridul Hemrajani  · 技术社区  · 1 年前

    我是Android开发的新手,正试图创建一个应用程序,点击一个按钮,我们就会被重定向到另一个活动。在VM中运行应用程序时,初始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)
                        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) 
    

    清单文件:

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

    这是我的主活动代码:

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

    以下是第二页:

     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 回复  |  直到 1 年前
        1
  •  0
  •   Ivan Wooll    1 年前

    这个问题是由机器人的灵活性引起的 TextView.setText() 功能。 它有多个版本,您可以传递许多不同的类型。 其中一个版本接受 Integer reference to a String resource.

    因为你传递的是一个由你的 generateRandomNumber() 函数这是使用的版本,因此此函数查找与id匹配的字符串资源,因此出现错误 Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5f

    要解决这个问题,您应该将文本的行设置更改为 luckyNumber.setText(String.valueOf(randomNumber)) 这将解决这个问题

    推荐文章