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

Android中RadioGroup取值与全局变量赋值问题求助

Fixing RadioGroup Selection & Volley Submission Issue

Hey there! Since you're new to Android and Java coming from C#, let's break down what's going wrong and fix this step by step.

First: The Core Problem in Your Radio Button Click Code

Looking at your current onRadioButtonClicked method, you're not actually assigning any value to your global category variable at all! Plus, the structure of your switch case has a common Java pitfall (fall-through) that would cause unexpected behavior even if you did add assignments.

Here's what's wrong with your original code:

  • You check if the radio button is checked, but then just break without setting category
  • If a radio button isn't checked (which happens automatically when another one is selected), the code will fall through to the next case, which isn't what you want

Corrected Click Event Code

First, make sure your global variable is initialized to avoid null values:

public String category = ""; // Initialize to empty string instead of leaving it unassigned

Then update your click handler to actually set the category value when a radio button is selected:

public void onRadioButtonClicked(View view) { 
    boolean checked = ((RadioButton) view).isChecked(); 

    // Only act when the radio button is being checked (since unchecking is automatic)
    if (checked) {
        switch (view.getId()) { 
            case R.id.film: 
                category = "film";
                break; 
            case R.id.actor: 
                category = "actor";
                break; 
            case R.id.actress: 
                category = "actress";
                break; 
            case R.id.editing: 
                category = "editing";
                break; 
            case R.id.effects: 
                category = "effects";
                break; 
            default:
                category = ""; // Fallback if no valid selection
                break;
        }
    }
}

Second: Fixing the ???? Submission Issue

Once category is correctly assigned, the ???? usually points to an encoding problem when sending the data via Volley. Make sure your request uses UTF-8 encoding to handle the string correctly.

Here's an example of a properly configured Volley POST request:

// Replace YOUR_SERVER_URL with your actual endpoint
StringRequest postRequest = new StringRequest(Request.Method.POST, YOUR_SERVER_URL,
    response -> {
        // Handle successful response here
    },
    error -> {
        // Handle error here
    }
) {
    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        params.put("category", category); // Now this has the correct selected value
        return params;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<>();
        // Ensure UTF-8 encoding for the request
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        return headers;
    }
};

// Add the request to Volley's queue
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(postRequest);

Quick Tips for C# Developers Moving to Java

  • Unlike C#, Java allows fall-through in switch cases (if you don't add break, code will continue to the next case). Always remember to add break after each case unless you intentionally want fall-through.
  • Uninitialized variables in Java can cause null pointer issues, so always initialize your global variables (like category = "" instead of just String category;).
  • Make sure each RadioButton in your layout has android:onClick="onRadioButtonClicked" set, otherwise the click event won't trigger.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:24:38