我正在创建一个简单的应用程序,每次去健身房都会产生锻炼程序。
每次我调用该方法生成一个训练字符串时,我都会得到一个IllegalStateException
这是我的第二个显示xml(我希望显示生成的String值训练)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="createRandomBicepWorkOut"
android:text="Generate"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="146dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<TextView
android:id="@+id/textView3"
android:layout_width="420dp"
android:layout_height="291dp"
android:text="TextView"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="254dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0"
tools:layout_editor_absoluteY="255dp" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
这是我的第二个显示java文件,其中我有生成训练的算法。
public class SecondDisplay extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seconddisplay); //xml file name
}
private TextView bicepResults;
public String createRandomBicepWorkOut(){
Random ran = new Random();
String[] bicepArray = new String[3];
String one = "curls";
String two = "preacher curls";
String three = "hammer curls";
String four = "barbell curls";
String five = "decline curls";
String six = "elliptical curls";
for(int i = 0; i < bicepArray.length;i++){
int index = ran.nextInt(5)+1;
if(index == 1)
bicepArray[i] = one;
else if(index == 2)
bicepArray[i] = two;
else if(index == 3)
bicepArray[i] = three;
else if(index == 4)
bicepArray[i] = four;
else if(index == 5)
bicepArray[i] = five;
else if(index == 6)
bicepArray[i] = six;
}
bicepResults = (TextView) findViewById(R.id.textView3);
for(int i = 0; i < bicepArray.length; i++){
bicepResults.setText(bicepArray[i]);
}
return "gains";
}
}
这是我的logcat
05-23 23:03:00.328 2686-2686/com.example.ghost.newapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ghost.newapp, PID: 2686
java.lang.IllegalStateException: Could not find method createRandomBicepWorkOut(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'button6'
at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4730)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4689)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:0)
如果您将点击方法设置为view
内的layout
,则需要使用以下参数View view
并返回void
(但不一定):< / p>
public void createRandomBicepWorkOut(View view){};