I'm new to Android Development and was trying to create an application where on clicking a button, we a re-directed to another activity. On running the app in a VM, the initial MainActivity runs with no errors but the SecondPage is not running.
Here are the error logs:
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)
Manifest File:
<?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>
This is the code for my MainActicity:
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);
}
}
Here is the SecondPage:
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"));
}
}
The problem is caused by the flexibility of androids TextView.setText()
function.
There are multiple versions of it that you can pass many different types to.
One of these versions accepts an Integer reference to a String resource.
Because you are passing it an Integer generated by your generateRandomNumber()
function this is the version that is used and so this function looks for a string resource matching the id, hence the error Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5f
To solve this you should change the line setting the text to luckyNumber.setText(String.valueOf(randomNumber))
and this will solve it