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

无法在方法括号内传入新List?TextView批量隐藏方法调用报错求助

Fixing "Cannot put a new List inside method brackets" Error & Simplifying TextView Visibility Setup

Got it, let's break down what's going on here and fix that error, plus make your code even cleaner as you wanted.

Why You're Seeing That Error

The core issue is that List is an interface in Java—you can't directly instantiate it with new List<>(). Interfaces define a contract but don't have concrete implementation code, so you need to use one of its concrete subclasses (like ArrayList) or a utility method that creates a valid List instance for you.

Correct Ways to Call Your Method

Here are a few valid ways to pass a List of TextViews directly in the method call:

Option 1: Use Arrays.asList() (Quick & Simple)

This creates a fixed-size List from your TextView instances—perfect if you don't need to modify the list later:

makeTextViewsInvisible(Arrays.asList(textView1, textView2, textView3));

Option 2: Use ArrayList (For Modifiable Lists)

If you might need to add/remove items from the list later, wrap the Arrays.asList() result in an ArrayList:

makeTextViewsInvisible(new ArrayList<>(Arrays.asList(textView1, textView2, textView3)));

Option 3: Java 9+? Use List.of()

Java 9 introduced a more concise way to create immutable lists:

makeTextViewsInvisible(List.of(textView1, textView2, textView3));

Simplify Your makeTextViewsInvisible Method Even More

Since your goal is to simplify code, let's trim that method down to be more readable and concise:

Enhanced For Loop (Clean & Readable)

Replace the while loop with an enhanced for loop—no need to track an index variable:

private void makeTextViewsInvisible(List<TextView> listOfTextViews) {
    for (TextView textView : listOfTextViews) {
        textView.setVisibility(View.INVISIBLE);
    }
}

Java 8+ Stream API (One-Liner Magic)

If you're using Java 8 or later, you can use forEach() to turn the method body into a single line:

private void makeTextViewsInvisible(List<TextView> listOfTextViews) {
    listOfTextViews.forEach(tv -> tv.setVisibility(View.INVISIBLE));
}

Skip the Method Entirely (Ultra-Simplified)

If you don't need to reuse this logic elsewhere, you can even combine the list creation and visibility setup into one line directly where you need it:

Arrays.asList(textView1, textView2, textView3).forEach(tv -> tv.setVisibility(View.INVISIBLE));

Quick Test Class Tip

When writing your test class, make sure you're initializing valid TextView instances to avoid null pointer exceptions. For example, in unit tests you can mock them:

@Test
void testMakeTextViewsInvisible() {
    TextView tv1 = Mockito.mock(TextView.class);
    TextView tv2 = Mockito.mock(TextView.class);
    
    makeTextViewsInvisible(Arrays.asList(tv1, tv2));
    
    Mockito.verify(tv1).setVisibility(View.INVISIBLE);
    Mockito.verify(tv2).setVisibility(View.INVISIBLE);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:39:27