这是oncreate函数我在这个方法中调用Appdata()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new DatabaseHelper(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
email=(EditText)findViewById(R.id.editText4);
pass=(EditText)findViewById(R.id.editText5);
conpass=(EditText)findViewById(R.id.editText6);
register=(Button)findViewById(R.id.btn_register);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
AddData();
}
public void AddData() {
register.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
boolean isInserted = db.insertData(email.getText().toString(),pass.getText().toString(), conpass.getText().toString());
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
在Appdata函数中OnClickListener()已创建,但我的问题是当我运行此程序应用程序变为停止并重新启动时 当我隐藏在Appdata()程序的代码中时,它的运行没有任何错误 我上传了图片click here
答案 0 :(得分:0)
在您的xml文件中没有存在id为R.id.btn_register的按钮,当您尝试设置单击侦听器时,这会导致NullPointerException。在xml布局中添加按钮和EditTexts,你就可以了。
在activity_main.xml
中添加以下代码<include
layout= "@layout/your_extra_layout_file"/>
你可以使用android:onclick =&#34; function_name&#34;属性与按钮 并在下面的活动中实现该功能。
public void function_name(View view) {
//do the action here
}