如何在android中的片段布局中的按钮单击事件上动态添加textview

时间:2016-01-01 09:23:09

标签: android android-fragments nullpointerexception

当我点击布局中的添加按钮时,我想创建一个TextView

为此我正在实现onClickListener并且在onlcik内部我已经为此编写了代码,但我在addView方法中获得了nullPointerException

public class ProfileTwoFragment extends Fragment implements View.OnClickListener {

    public ProfileTwoFragment(){

    }

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

    DBHelper db;
    Context context;
    EditText ftwo_designation,ftwo_organization,ftwo_since;
    Button add;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_profile_two, container, false);

        context=getContext();
        db=new DBHelper(context);

        ftwo_designation = (EditText)view.findViewById(R.id.ftwo_designation);
        ftwo_organization = (EditText)view.findViewById(R.id.ftwo_organization);

        add=(Button)view.findViewById(R.id.button);
        //container = (LinearLayout)view.findViewById(R.id.container);

       add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                LinearLayout ll = (LinearLayout)v.findViewById(R.id.linearLayout2);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                TextView tv = new TextView(getActivity().getApplicationContext());
                tv.setTextSize(15);
                tv.setText("test adding");
                tv.setLayoutParams(lp);
                ll.addView(tv);

            }
        });

        String temp = db.getDesignation(1);
        ftwo_designation.setText(temp);

        String temp1 = db.getOrganization(1);
        ftwo_organization.setText(temp1);

        ftwo_designation.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {




            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String des = (String)ftwo_designation.getText().toString();
                db.insertDesigntion(des,1);
                Toast.makeText(getActivity(), "Saved Successfully", Toast.LENGTH_LONG).show();

            }

            @Override
            public void afterTextChanged(Editable s) {
                db.updateDesignation(s.toString(),1);
            }
        });
        ftwo_organization.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String org = (String)ftwo_organization.getText().toString();
                db.insertOrganization(org,1);
                Toast.makeText(getActivity(),"Saved Successfully", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void afterTextChanged(Editable s) {
                db.updateOrganization(s.toString(), 1);
            }
        });



        return view;
    }

    @Override
    public void onClick(View v) {


    }
}

现在我正在nullPointerException方法获得addView 请帮忙。

1 个答案:

答案 0 :(得分:1)

错误就在这一行

LinearLayout ll = (LinearLayout)v.findViewById(R.id.linearLayout2);

您无法使用v,R.id .linearLayout2位于您的主要布局中,因此您需要使用view

 LinearLayout ll = (LinearLayout)view.findViewById(R.id.linearLayout2);

从代码开始,您将无法访问view内的onClick,因此请将其声明为finalonCreate()