使用setID时FindViewByID无法正常工作

时间:2014-01-03 04:43:59

标签: java android xml findviewbyid

我想以编程方式创建 EditText 字段,然后确保输入不为null,我一直在使用setId为字段创建id,然后使用{ {1}}但由于某种原因,我觉得它没有找到Id。每当我在手机上加载应用程序时,我就会进入"添加"部分,如果我点击它,它会崩溃。

此外,findViewById(int)是否将 EditText 设置为TEST哪个不是我认为我可能在查找ID /集ID时出错?

eweight.setText("TEST")

4 个答案:

答案 0 :(得分:0)

替换

   EditText eweight = (EditText) findViewById(99);
   EditText emark = (EditText) findViewById(100);

  AlertDialog alertDialog = (AlertDialog)dialog;
  EditText eweight = (EditText) alertDialog.findViewById(99);
  EditText emark = (EditText) alertDialog.findViewById(100);

当您说findViewById()时,它会在View窗口中搜索Activity,如果您说alertDialog.findViewById(),则会在AlertDialog窗口中搜索您的布局是AlertDialog窗口的一部分,而不是Activity窗口......

答案 1 :(得分:0)

你的findViewById()方法给出null,因为它在你的活动布局中找到了视图,但你已经在alertdialog布局上设置了id,所以你应该这样做来运行你的代码: -

 EditText eweight = (EditText) layout.findViewById(99);
 EditText emark = (EditText) layout.findViewById(100);

它有效..!

答案 2 :(得分:0)

// try this way 
 private EditText weight;
    private  EditText mark;
    @Override
    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout layout = new LinearLayout(this);
        weight = new EditText(this);
        mark = new EditText(this);
        mark.setInputType(InputType.TYPE_CLASS_NUMBER);
        weight.setInputType(InputType.TYPE_CLASS_NUMBER);

        weight.setId(99);
        mark.setId(100);

        layout.addView(mark);
        layout.addView(weight);

        AlertDialog.Builder addwork = new AlertDialog.Builder(this);
        addwork.setView(layout);

        addwork.setPositiveButton("Add", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                String coursename = ecoursename.getText().toString();
                weight.setText("TEST");
                try {
                    if ((weight.getText().toString().trim().equals("")) ||
                            (mark.getText().toString().trim().equals(""))) {
                    } else {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast invalidinputtoast = Toast.makeText(getApplicationContext(),
                            "Please input a value", Toast.LENGTH_LONG);
                    invalidinputtoast.show();
                }

            }
        });
    }

答案 3 :(得分:0)

您在alertDialog中添加了视图,因此在活动视图中找到关于该视图的编辑文本的ID。

 AlertDialog.Builder addwork = new AlertDialog.Builder(this);
 addwork.setView(layout);
 addwork.setPositiveButton("Add", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        EditText eweight = (EditText) addwork. findViewById(99);//changes here 
        EditText emark = (EditText)addwork.findViewById(100);
        String coursename = ecoursename.getText().toString();
        eweight.setText("TEST");


        try {
            if ((eweight.getText().toString().trim().equals("")) || 
                    (emark.getText().toString().trim().equals(""))) {
                throw new InvalidInputException();
            } else {
            (irrelevant code removed)


        } catch (InvalidInputException e) {
            e.printStackTrace();
            Toast invalidinputtoast = Toast.makeText(getApplicationContext(),
                    "Please input a value", Toast.LENGTH_LONG);
            invalidinputtoast.show();
        }