【问题标题】:Android - Void methods cannot return a valueAndroid - Void 方法不能返回值
【发布时间】:2015-04-30 10:20:15
【问题描述】:

我有一个带有表格布局的活动。我将文本从字符串数组放入布局中。但现在我收到错误:“无效方法无法返回值”

public class stop10 extends SherlockFragmentActivity {

String[] column1;
String[] column2;
View view;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stop10);

    TableLayout prices = (TableLayout)view.findViewById(R.id.table);
    String[] column1 = getResources().getStringArray(R.array.column1);
    String[] column2 = getResources().getStringArray(R.array.column2);
    prices.setStretchAllColumns(true);
    prices.bringToFront();
    for(int i = 0; i < column1.length; i++){
        TableRow tr =  new TableRow(getApplicationContext());
        TextView c1 = new TextView(getApplicationContext());
        c1.setText(column1[i]);
        TextView c2 = new TextView(getApplicationContext());
        c2.setText(column2[i]);
        tr.addView(c1);
        tr.addView(c2);
        prices.addView(tr);

    }
    return view;

}

我知道void方法不能返回值,但是我现在还应该使用什么?

提前致谢。

【问题讨论】:

  • 如果上面写着Void methods cannot return a value,那么你应该删除return view;

标签: android methods tablelayout void


【解决方案1】:

以这种方式初始化表格布局

TableLayout prices = (TableLayout)findViewById(R.id.table);

同时删除

return view;

您不必在 oncreate 方法中返回视图。

setContentView(R.layout.stop10);

处理视图。

【讨论】:

    猜你喜欢
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    相关资源
    最近更新 更多