Android MyApps崩溃,但找不到解决方案,
//这是我的日志,找不到解决方案Android MyApps崩溃,但找不到解决方案,,android,android-studio,Android,Android Studio,//这是我的日志,找不到解决方案 09-17 07:56:55.115 6180-6180/com.example.safwan.mymonitoring E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.safwan.mymonitoring, PID: 6180 java.lang.IllegalStateException: Could not find method BSave(View) in a
09-17 07:56:55.115 6180-6180/com.example.safwan.mymonitoring E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.safwan.mymonitoring, PID: 6180
java.lang.IllegalStateException: Could not find method BSave(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'BSave'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
//RecordActivity.java,其中用于将数据插入MYSQL的代码
公共类RecordActivity扩展了AppCompatActivity{
Button BSave;
EditText TFincubatorid, TFTotal, TFDate;
ProgressDialog progressDialog;
AlertDialog.Builder builder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_record);
//initializa alert dialoog
//builder = new AlertDialog.Builder(RecordActivity.this);
//initialize by text field
TFincubatorid = (EditText) findViewById(R.id.TFincubatorid);
TFTotal = (EditText) findViewById(R.id.TFTotal);
TFDate = (EditText) findViewById(R.id.TFDate);
//initialize btn by id
BSave = (Button) findViewById(R.id.BSave);
// click function
BSave.setOnClickListener((View.OnClickListener) this); //click button
// pop up loading dialog
progressDialog = new ProgressDialog(this);
}
//按钮功能,用户点击此处保存到数据库中
已经三天了
public void BSave (View view)
{
final String incubatorid = TFincubatorid.getText().toString().trim();
final String total = TFTotal.getText().toString().trim();
final String calender = TFDate.getText().toString().trim();
//validation
if (TextUtils.isEmpty(incubatorid))
{
TFincubatorid.setError("Fill in incubator id");
TFincubatorid.requestFocus();
return;
}
if (TextUtils.isEmpty(total))
{
TFTotal.setError("Fill in total succesfull egg hatch");
TFTotal.requestFocus();
return;
}
if (TextUtils.isEmpty(calender))
{
TFDate.setError("Choose date after 21 days");
TFDate.requestFocus();
return;
}
class Record extends AsyncTask<Void, Void, String>
{
@Override
public void onPreExecute()
{
progressDialog = ProgressDialog.show(RecordActivity.this, "Loading Data", null, true, true);
}
@Override
protected String doInBackground(Void... v)
{
HashMap<String, String> params = new HashMap<>();
params.put(constant.TFincubatorid, incubatorid);
params.put(constant.TFTotal, total);
params.put(constant.Calender, calender);
RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(constant.ROOT_URLRECORD, params);
return res;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
if (s.equalsIgnoreCase("You have been successfully save the data."))
{
Toast.makeText(RecordActivity.this, s, Toast.LENGTH_LONG).show();
Intent intent = new Intent (RecordActivity.this, RecordActivity.class);
finish();
startActivity(intent);
}
else
{
Toast.makeText(RecordActivity.this, s, Toast.LENGTH_LONG).show();
}
}
}
Record reg = new Record();
reg.execute();
}
}有两种方法可以处理按钮点击, 首先是通过在XML中为按钮设置
onClick
属性来实现回调OnClickListener
第一种方法是这样的
Button bSave = (Button) findViewById(R.id.BSave);
// Register click listener
BSave.setOnClickListener(this);
您必须使您的活动或片段实现OnClickListener
如果要以另一种方式执行,请将这一行添加到您的按钮中,android:onClick=“BSsave”
,您编写的方法将起作用
注意:在这种情况下,要调用的onClick
方法必须是公共的,并且应该有一个视图
参数,如下所示:
public void yourMethodName(View view) {
// do something here...
// you can check which button is clicked by checking the id
if (view.getId() == R.id.myButton1) {
// Button1 clicked
}
}
显示您的代码。永远不要在XML中设置onClick侦听器。这就是你的错误来源。看看这个显示你的xml代码以及上面的所有代码,帮我一把如果它不起作用我已经试过了但按钮不起作用或者我的应用程序崩溃了,我试过第一种方法,但我的应用程序崩溃了,第二种方法我也试过了,但我的按钮不能点击我现在很纳闷,当我创建RecordActivity时,它从未扩展或链接到Record_Fragment.xml和RecordFragment.java。那么您认为这可能是错误原因吗?请删除这一行,
BSave.setOnClickListener((View.OnClickListener)this)//单击按钮
很抱歉,我无法理解您的评论,“我现在想知道,当我创建RecordActivity时,java从未扩展或链接到Record_Fragment.xml和RecordFragment.java。那么您认为这可能是错误原因吗?”
Button bSave = (Button) findViewById(R.id.BSave);
// Register click listener
BSave.setOnClickListener(this);
public void yourMethodName(View view) {
// do something here...
// you can check which button is clicked by checking the id
if (view.getId() == R.id.myButton1) {
// Button1 clicked
}
}