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

Android Studio中TextView无法显示传递的字符串问题求助

Fixing TextView Not Showing Passed String in Second Activity

Hey there! Let's troubleshoot why your TextView isn't displaying the string from Main Activity. I'll break down the most common issues and how to resolve them step by step:

1. Double-Check Intent Data in Main Activity

First, make sure you're correctly packaging the input string into the Intent. Here's what the proper code should look like (replace with your actual EditText ID):

// In MainActivity, after getting input from your EditText
EditText inputEditText = findViewById(R.id.your_input_edit_text);
String inputString = inputEditText.getText().toString();

// Create Intent and pass the data
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("USER_INPUT", inputString); // Use a consistent, case-sensitive key!
startActivity(intent);

Common mistake here: Forgetting to extract text from the EditText, or using a typo in the extra key (like mixing uppercase/lowercase).

2. Verify Data Receiving in Second Activity

Next, ensure you're correctly retrieving the data and binding it to your TextView. The order is critical—always call setContentView before initializing your TextView:

// In SecondActivity's onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second); // This must come first!

    // Initialize your TextView (match the ID from your layout XML exactly)
    TextView resultTextView = findViewById(R.id.your_result_text_view);

    // Get the incoming Intent and extract data
    Intent receivedIntent = getIntent();
    if (receivedIntent != null) {
        String receivedText = receivedIntent.getStringExtra("USER_INPUT"); // Same key as MainActivity!
        
        // Debug check: Log the received text to confirm it's not null
        Log.d("SecondActivity", "Received text: " + receivedText);

        if (receivedText != null) {
            resultTextView.setText(receivedText);
        } else {
            resultTextView.setText("No input received"); // Fallback for empty input
        }
    }
}

3. Check for Layout & Visibility Issues

Even if data is passed correctly, your TextView might be hidden or unreadable due to layout problems:

  • Mismatched TextView ID: Ensure the ID in findViewById exactly matches android:id="@+id/your_result_text_view" in your activity_second.xml.
  • Visibility: Confirm your TextView has android:visibility="visible" (it's the default, but it's worth double-checking).
  • Color Conflict: If your text color matches the background, it'll look like nothing's there. Try setting android:textColor="@android:color/black" if your background is white.
  • Overlapping Views: Make sure no other views (like a Button or another TextView) are covering your result TextView in the layout.

4. Quick Debugging Tips

  • Use Logcat to check if the received text is correct. The log line above will tell you if the string is being passed properly.
  • If the app runs but shows nothing, check if you actually entered text in Main Activity's input field (empty strings will make the TextView look blank).

Try these steps, and your TextView should start displaying the passed string in no time! If you're still stuck, share your full SecondActivity code and layout XML, and I can help pinpoint the exact issue.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:53:40