The error:
java.lang.IllegalStateException: Could not find method onButtonClick (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'bSetTime'
I have only made one method in java.
When I select the second onButtonClick in Design the app works, but when I restart the app it crashes again and it has the first method selected. I can then select the second option again and restart the app. to repeat this process.
In other answers it was suggested to just use: button.setOnClickListener
.
It works, but I'd like to learn from this.
The java File:
package com.example.halfacht.pickers;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* TIME PICKER */
TimePicker tp = (TimePicker) findViewById(R.id.timePicker);
}
// Button
public void onButtonClick(View e) {
// TextView tvSetTime = (TextView) findViewById(R.id.textView);
// TimePicker tp = (TimePicker) findViewById(R.id.timePicker);
// tvSetTime.setText(tp.getCurrentHour().toString() + ":" + tp.getCurrentMinute().toString());
}
}
My XML File:
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/timePicker"
android:timePickerMode="spinner" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_alignBaseline="@+id/bSetTime"
android:layout_alignBottom="@+id/bSetTime"
android:layout_toRightOf="@+id/bSetTime"
android:layout_toEndOf="@+id/bSetTime"
android:layout_marginLeft="54dp"
android:layout_marginStart="54dp" />
<Button
android:text="Set Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:id="@+id/bSetTime"
android:layout_below="@+id/timePicker"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onButtonClick (MainActivity)" />
</RelativeLayout>
Last line in your XML. Change this:
to this: