我正在片段中启动我的活动,但变量变为空?
Posted
技术标签:
【中文标题】我正在片段中启动我的活动,但变量变为空?【英文标题】:I am initiating my activity in a fragment but the variables are getting null? 【发布时间】:2018-03-04 11:29:04 【问题描述】:这是活动类的代码。
public class Form1Activity extends MainActivity
private EditText factory_name,cell_no, _email:
private Spinner meter_type_spinner;
private Button form1submitbutton;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
factory_name = (EditText) findViewById(R.id.factorynameTF);
cell_no = (EditText) findViewById(R.id.cellnoTF);
_email = (EditText) findViewById(R.id.emailTF);
meter_type_spinner = (Spinner) findViewById(R.id.metertypedropdown);
form1submitbutton = (Button) findViewById((R.id.form1submitbtn));
List<String> metertype = new ArrayList<>();
metertype.add("day");
metertype.add("night");
ArrayAdapter<String> adpterMeterType = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, metertype);
adpterMeterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
meter_type_spinner.setAdapter(adpterMeterType);
这是我调用活动的片段代码。知道请告诉我我做错了什么。我应该在片段中处理所有字段还是为每个片段进行单独的活动以执行视图的工作,例如编辑 tex、微调器等。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.form1, container, false);
context = rootView.getContext();
Intent intent = new Intent(context, Form1Activity.class);
startActivity(intent);
return rootView;
【问题讨论】:
【参考方案1】:试试:
Intent intent = new Intent(getActivity(), Form1Activity.class);
getActivity().startActivity(intent);
而不是
`Intent intent = new Intent(context, Form1Activity.class)`;
startActivity(intent);
【讨论】:
以上是关于我正在片段中启动我的活动,但变量变为空?的主要内容,如果未能解决你的问题,请参考以下文章