单击时,从TableLayout中动态创建的EditText获取用户输入的文本

时间:2014-06-07 18:31:54

标签: java android android-edittext tablelayout tablerow

我正在编写一个应用程序,用户从NumberPicker中选择一个数字(例如2)。然后,创建了一些EditTexts(在这种情况下为2)并单独添加到TableRows,这些TableLayout被添加到EditTexts,最终成为{{1}的列用户可以选择输入他们的名字。

单击按钮,我想将名称(如果已输入)编译到ArrayList中,并将ArrayList传递给下一个Activity。

但是当我使用NullPointerException

时,我不断获得editText.getText().toString().trim()

任何人都可以告诉我为什么或如何解决它?我是Java的新手,所以我的编码可能很残忍......

package com.bowman.chalkone.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.ArrayList;

public class SecondaryFragment extends Fragment implements NumberPicker.OnValueChangeListener, View.OnClickListener {

    Button backBtn, nextBtn;
    TableLayout tableLayout;
    NumberPicker numPicker;
    Integer numPlayers;

    public SecondaryFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_secondary, container, false);

        //find buttons and set onclicklistener
        backBtn = (Button) v.findViewById(R.id.backBtn);
        nextBtn = (Button) v.findViewById(R.id.nextBtn);

        //what happens when the "back" button is clicked
        backBtn.setOnClickListener(this);

        //what happens when the "next" button is clicked
        nextBtn.setOnClickListener(this);

        //find numberpicker and set onvaluechangedlistener
        numPicker = (NumberPicker) v.findViewById(R.id.numberPicker);
        numPicker.setOnValueChangedListener(this);
        //set min & max
        numPicker.setMinValue(2);
        numPicker.setMaxValue(12);

        //find tablelayout
        tableLayout = (TableLayout) v.findViewById(R.id.midTblLyt);

        //allow second column to stretch (the editText fields)
        tableLayout.setColumnStretchable(1, true);
        //reset "name" fields to default of two
        addPlayerNames(numPicker.getMinValue());

        return v;
    }

    private ArrayList<String> getPlayerNames() {

        //create new ArrayList to store player names
        ArrayList<String> playerArray = new ArrayList<String>();

        //number of players added used as player number if no name
        Integer playersAdded = 0;

        for (int i = 0, c = tableLayout.getChildCount(); i < c; i++) {

            TableRow tableRow = (TableRow) tableLayout.getChildAt(i);

            for (int j = 0, d = tableRow.getChildCount(); j < d; j++) {

                Error -> EditText editText = (EditText) tableRow.getChildAt(d);
                String name = editText.getText().toString().trim();

                //increment playersAdded (because adding will happen)
                playersAdded++;

                //check if "name" is empty
                if (TextUtils.isEmpty(name)) {
                    //since it is, add default "Player #"
                    playerArray.add("Player " + playersAdded);
                } else {
                    //since it is not, add "name"
                    playerArray.add(name);
                }
            }
        }

        return playerArray;
    }

    public static SecondaryFragment newInstance() {

        //create new fragment
        SecondaryFragment f = new SecondaryFragment();

        return f;
    }

    @Override
    public void onValueChange(NumberPicker numberPicker, int i, int i2) {
        //add the required "name" fields
        addPlayerNames(i2);
    }

    private void addPlayerNames(Integer players) {

        //initiate variables
        int itter = 0;
        numPlayers = players;

        //clear everything out of tableLayout in case of previous population
        tableLayout.removeAllViewsInLayout();

        //populate tableLayout with labels and textedits
        while (itter < numPlayers) {

            //increment itter
            itter++;

            //initialize elements
            TableRow tableRow = new TableRow(getActivity().getApplicationContext());
            tableRow.setBottom(10);

            TextView textView = new TextView(getActivity().getApplicationContext());
            textView.setTextSize(20);
            textView.setPadding(0, 10, 0, 0);
            textView.setRight(5);
            textView.setText("Player " + itter + ":");

            EditText editText = new EditText(getActivity().getApplicationContext());

            //add textView and editText to tableRow
            tableRow.addView(textView, 0);
            tableRow.addView(editText, 1);

            //add tableRow to tableLayout
            tableLayout.addView(tableRow);
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.backBtn: {

                //go to PrimaryFragment page
                MainActivity.pager.setCurrentItem(0);

                //reset numberpicker to default minvalue
                numPicker.setValue(numPicker.getMinValue());

                //reset default number of "name" fields
                addPlayerNames(numPicker.getMinValue());

            }
            break;

            case R.id.nextBtn: {

                Error -> ArrayList < String > playerArray = getPlayerNames();

                //start ScoreActivity
                Intent intent = new Intent(getActivity().getApplicationContext(), ScoreActivity.class);
                intent.putStringArrayListExtra("arrayList", playerArray);

                startActivity(intent);

            }
            break;
        }
    }
}

Logcat输出:

  

06-07 14:49:15.026 14181-14181 / com.bowman.chalkone.app   D / AndroidRuntime:关闭VM           06-07 14:49:15.026 14181-14181 / com.bowman.chalkone.app W / dalvikvm:threadid = 1:线程退出未捕获的异常   (组= 0x4163ace0)           06-07 14:49:15.028 14181-14181 / com.bowman.chalkone.app E / AndroidRuntime:FATAL EXCEPTION:main       过程:com.bowman.chalkone.app,PID:14181       显示java.lang.NullPointerException               在com.bowman.chalkone.app.SecondaryFragment.getPlayerNames(SecondaryFragment.java:82)               在com.bowman.chalkone.app.SecondaryFragment.onClick(SecondaryFragment.java:172)               在android.view.View.performClick(View.java:4445)               在android.view.View $ PerformClick.run(View.java:18446)               在android.os.Handler.handleCallback(Handler.java:733)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:136)               在android.app.ActivityThread.main(ActivityThread.java:5144)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)               at dalvik.system.NativeStart.main(Native Method)           06-07 14:49:16.825 14181-14181 / com.bowman.chalkone.app I / Process:发送信号。 PID:14181 SIG:9

1 个答案:

答案 0 :(得分:0)

tr.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        TableRow t = (TableRow) v;
        EditText firstText = (EditText ) t.getChildAt(position of edittext in table row);
        String sCode = firstText.getText().toString();
        Log.v("Inserted---->", sCode  );
    }
});