【问题标题】:OnClickListener() doesn't allow me to start the programOnClickListener() 不允许我启动程序
【发布时间】:2016-10-12 16:17:04
【问题描述】:

这是在此方法中调用 Appdata() 的 oncreate 函数

    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

【问题讨论】:

  • 发布您在 Android 监视器上看到的错误消息
  • 不...它只在模拟器中显示“应用程序不断停止”
  • 您在 Android 监视器上看不到故障转储?
  • no..我试图将 onclicklistener 放入 OnCreate() 但它不起作用
  • 发布你的activity_main.xml

标签: sqlite android-studio onclicklistener


【解决方案1】:

在您的 xml 文件中不存在 ID 为 R.id.btn_register 的按钮,这会在您尝试设置单击侦听器时导致 NullPointerException。在您的 xml 布局中添加按钮和 EditTexts,您就可以开始了。

在您的 activity_main.xml 中添加以下代码

<include
     layout= "@layout/your_extra_layout_file"/>

您可以在按钮上使用 android:onclick="function_name" 属性 并在 Activity 中实现该功能,如下所示。

public void function_name(View view) {
     //do the action here
}

【讨论】:

  • 它可以工作...但是假设,我想在 my_extra_layout 中编码(onclicklistener())怎么做?
  • i64.tinypic.com/eqo8pj.png 这又发生了 activity_main 的设计过载
  • 我将 AddData() 放在 my_extra_layout 中,但是当我点击注册(按钮)时出现问题
猜你喜欢
  • 1970-01-01
  • 2021-04-26
  • 2013-10-07
  • 2023-03-10
  • 1970-01-01
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多