将值从stringbuilder传递到textview

时间:2015-04-24 10:09:29

标签: android stringbuilder

我正在尝试从多个EditTexts获取字词,并使用TextView显示它们而不使用任何布局。但是每次运行程序都会强制关闭程序。

  

LOGCAT:java.lang.RuntimeException:无法实例化活动   ComponentInfo {com.example.tablelayout / com.example.tablelayout.MainActivity}:   显示java.lang.NullPointerException

public class MainActivity extends Activity {
    private List<EditText> editTextList = new ArrayList<EditText>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout linearLayout = new LinearLayout(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
        linearLayout.setLayoutParams(params);
        linearLayout.setOrientation(VERTICAL);

        int count = 10;
        linearLayout.addView(tableLayout(count));
        linearLayout.addView(submitButton());
        setContentView(linearLayout);
    }

    private Button submitButton() {
        Button button = new Button(this);
        button.setHeight(WRAP_CONTENT);
        button.setText("Submit");
        button.setOnClickListener(submitListener);
        return button;
    }

    TextView txt=new TextView(this);



    // Access the value of the EditText

    private View.OnClickListener submitListener = new View.OnClickListener() {
        public void onClick(View view) {
            StringBuilder stringBuilder = new StringBuilder();
            for (EditText editText : editTextList) {
                stringBuilder.append(editText.getText().toString());


            }

            txt.setText(stringBuilder.toString().trim());
        }
    };

    // Using a TableLayout as it provides you with a neat ordering structure

    private TableLayout tableLayout(int count) {
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setStretchAllColumns(true);
        int noOfRows = count / 5;
        for (int i = 0; i < noOfRows; i++) {
            int rowId = 5 * i;
            tableLayout.addView(createOneFullRow(rowId));
        }
        int individualCells = count % 5;
        tableLayout.addView(createLeftOverCells(individualCells, count));
        return tableLayout;
    }

    private TableRow createLeftOverCells(int individualCells, int count) {
        TableRow tableRow = new TableRow(this);
        tableRow.setPadding(0, 10, 0, 0);
        int rowId = count - individualCells;
        for (int i = 1; i <= individualCells; i++) {
            tableRow.addView(editText(String.valueOf(rowId + i)));
        }
        return tableRow;
    }

    private TableRow createOneFullRow(int rowId) {
        TableRow tableRow = new TableRow(this);
        tableRow.setPadding(0, 10, 0, 0);
        for (int i = 1; i <= 5; i++) {
            tableRow.addView(editText(String.valueOf(rowId + i)));
        }
        return tableRow;
    }

    private EditText editText(String hint) {
        EditText editText = new EditText(this);
        editText.setId(Integer.valueOf(hint));
        editText.setHint(hint);
        editTextList.add(editText);
        return editText;
    }
}

2 个答案:

答案 0 :(得分:1)

  TextView txt=new TextView(this);

您无法同时声明和初始化类成员视图,因为Contextnot yet valid

将其更改为:

public class MainActivity extends Activity {
    private List<EditText> editTextList = new ArrayList<EditText>();
    private TextView txt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        txt = new TextView(this);
        // other code

我也注意到您没有将此View添加到您提供给setContentView的任何容器中。该建议将解决崩溃,但您不会看到TextView的内容

答案 1 :(得分:-1)

异常显示问题出在您的清单文件中。 您的包名称是清单标记,您的活动位于不同的位置。