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

Kotlin中Activity间信息传递异常:原有获取参数方法失效

Fixing Intent Extra Retrieval in KullaniciOlustur2

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 makeUsername and makeUserphone are properly initialized (either via findViewById or 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 makeUsername in KullaniciOlustur2 is null, setting .text will silently fail (or crash). Always confirm your views are initialized after setContentView().
  • 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:38:57