我的按钮方法有一些问题"添加" ,因为该活动正在调用另一个类,我无法找到错误的位置。
AddPropiedadActivity.java包含下一个代码:
public class AddPropiedadActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_propiedad);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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();
setContentView(R.layout.activity_add_propiedad);
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void add(View view){
Toast.makeText(getBaseContext(), "TESTING ", Toast.LENGTH_SHORT).show();
}
}
活动content_add_propiedad.xml,只有一个带onClick方法的按钮
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_add_propiedad"
tools:context="com.example.ivy.bienesraicesapp.AddPropiedadActivity">
<Button
android:onClick="add"
android:id="@+id/btn_add"
android:text="@string/titulo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
但是当我点击按钮得到这个错误,因为调用PanelUsuarioActivity而不是AddPropiedadActivity,即使我在PanelUsuarioActivity中添加我的方法按钮工作...所以我想修复此错误,并且活动content_add_propiedad调用java类AddPropiedadActivity:
java.lang.IllegalStateException:找不到方法add(View)in 活动课 onClick的com.example.ivy.bienesraicesapp.PanelUsuarioActivity 视图类android.support.v7.widget.AppCompatButton上的处理程序 id&#39; btn_add&#39;
这是另一个java类
public class PanelUsuarioActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_panel_usuario);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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();
setContentView(R.layout.activity_add_propiedad);
}
});
}
}
感谢。
答案 0 :(得分:0)
当我使用setContentView调用Activity时,我的错误出现在PanelUsuarioActivity中。所以我用一种方法改变它,它的工作原理! :!)强>
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();
openAlta();
}
});
}
public void openAlta( ){
Intent intent = new Intent(this,AddPropiedadActivity.class);
startActivity(intent);
}