Kotlin中Activity间信息传递异常:原有获取参数方法失效
Hey there, let's troubleshoot why your Intent extras aren't showing up in KullaniciOlustur2. Let's break this down step by step:
First, Verify the Sender Code (MainActivity)
Your sender code looks correct at first glance, but let's double-check a couple of critical details:
- Make sure
makeUsernameandmakeUserphoneare properly initialized (either viafindViewByIdor View Binding) before calling.text.toString(). If they're null, you'll be sending empty strings without realizing it. - Add debug logs to confirm you're actually sending valid data:
fun kullaniciOlustur2(view: View){ val username = makeUsername.text.toString() val phone = makeUserphone.text.toString() // Add these logs to check what's being sent Log.d("MainActivity", "Sending username: $username") Log.d("MainActivity", "Sending phone: $phone") val intent = Intent(applicationContext,KullaniciOlustur2::class.java) intent.putExtra("input", username) intent.putExtra("input2", phone) startActivity(intent) }
Fix the Receiver Code (KullaniciOlustur2)
The issue likely stems from how or where you're retrieving the Intent. Here are the correct approaches for common scenarios:
Case 1: Standard Launch Mode (Most Common)
Make sure you're fetching the Intent inside onCreate() after setting the content view, and add null safety checks to avoid crashes:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kullanici_olustur2) // Initialize your TextViews first (this is critical!) val makeUsername = findViewById<TextView>(R.id.your_username_textview_id) // Replace with your actual ID val makeUserphone = findViewById<TextView>(R.id.your_phone_textview_id) // Get the incoming Intent (both `intent` property and `getIntent()` work here) val incomingIntent = intent ?: getIntent() // Retrieve extras with default values to handle empty/missing data val receivedUsername = incomingIntent.getStringExtra("input") ?: "No username received" val receivedPhone = incomingIntent.getStringExtra("input2") ?: "No phone received" // Update the TextViews makeUsername.text = receivedUsername makeUserphone.text = receivedPhone // Add logs to verify what's being received Log.d("KullaniciOlustur2", "Received username: $receivedUsername") Log.d("KullaniciOlustur2", "Received phone: $receivedPhone") }
Case 2: Launch Mode is singleTask/singleInstance
If you've set android:launchMode="singleTask" or singleInstance for KullaniciOlustur2 in your AndroidManifest.xml, new Intents will trigger onNewIntent() instead of onCreate(). You need to update the activity's Intent reference here:
override fun onNewIntent(intent: Intent?) { super.onNewIntent(intent) setIntent(intent) // Update the activity's current Intent reference // Re-fetch and apply the extras like you did in onCreate val receivedUsername = intent?.getStringExtra("input") ?: "No username received" makeUsername.text = receivedUsername }
Common Pitfalls to Check
- Typos in Extra Keys: Ensure "input" and "input2" are spelled exactly the same in both sender and receiver (they're case-sensitive!).
- Uninitialized Views: If
makeUsernamein KullaniciOlustur2 is null, setting.textwill silently fail (or crash). Always confirm your views are initialized aftersetContentView(). - Empty Extras: If the EditTexts in MainActivity are empty, you'll send empty strings. The debug logs we added will help you spot this immediately.
Try these steps, and the logs should point you straight to the root of the issue!
内容的提问来源于stack exchange,提问作者Ceridoglu

