gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:40:37 25 4
gpt4 key购买 nike

我想在我点击布局中的添加按钮时创建一个 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) {


}
}

现在我在 addView 方法中得到一个 nullPointerException请帮忙。

最佳答案

错误在这一行

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

你不能使用v,R.id.linearLayout2 在你的主布局中所以你需要使用view

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

根据您的代码,您将无法在 onClick 内访问 view,因此将其声明为 final 或在 onCreate 之外()

关于android - 如何在android中的 fragment 布局中的按钮单击事件上动态添加textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34554955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com