I'm trying to implement an application using flipper. So when you click on the button displays the following error:

FATAL EXCEPTION: main java.lang.IllegalStateException: Could not find method getResultQuad(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'getResult' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) at android.view.View.performClick(View.java:4211) at android.view.View$PerformClick.run(View.java:17446) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:153) at android.app.ActivityThread.main(ActivityThread.java:5297) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) 

Here is the code itself:

 public class QuadEquation extends Activity implements View.OnClickListener { Button getResult; public void QuadDiffer() { TextView result = (TextView) findViewById(R.id.result); EditText koefA = (EditText) findViewById(R.id.koefA); EditText koefB = (EditText) findViewById(R.id.koefB); EditText koefC = (EditText) findViewById(R.id.koefC); int a = Integer.parseInt(koefA.getText().toString()); int b = Integer.parseInt(koefB.getText().toString()); int c = Integer.parseInt(koefC.getText().toString()); int diskrim = b * b - 4 * a * c; if (diskrim < 0) { Toast toast = Toast.makeText(getApplicationContext(), "Дискриминант < 0. Корни отсутствуют", Toast.LENGTH_SHORT); toast.show(); } else { double x1 = (double) (-b + Math.sqrt(diskrim) / 2 * a); double x2 = (double) (-b - Math.sqrt(diskrim) / 2 * a); result.setText("x1 = " + x1 + "and x2 = " + x2); } getResult.findViewById(R.id.result); getResult.setOnClickListener(this); } @Override public void onClick(View view) { QuadDiffer(); } } 

 `public class MainActivity extends AppCompatActivity implements` View.OnTouchListener { ViewFlipper flipper; float fromPosition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout); mainLayout.setOnTouchListener(this); flipper = (ViewFlipper)findViewById(R.id.flipper); LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); int [] layouts = new int[] {R.layout.quad_activity, R.layout.calc_activity, R.layout.third_activity}; for(int layout : layouts) { flipper.addView(inflater.inflate(layout, null)); } } @Override public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: fromPosition = event.getX(); break; case MotionEvent.ACTION_UP: float toPosition = event.getX(); if(fromPosition > toPosition) { flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.go_next_in)); flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.go_next_out)); flipper.showNext(); } else if (fromPosition < toPosition) { flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.go_prev_in)); flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.go_prev_out)); flipper.showPrevious(); } default: break; } return true; } 

}

XML:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <EditText android:layout_width="170dp" android:layout_height="wrap_content" android:inputType="number" android:ems="10" android:id="@+id/koefA" android:hint="Enter here first koef" android:layout_above="@+id/koefB" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginBottom="44dp" /> <EditText android:layout_width="170dp" android:layout_height="wrap_content" android:inputType="number" android:ems="10" android:id="@+id/koefB" android:hint="Enter here second koef" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginBottom="46dp"/> <EditText android:layout_width="170dp" android:layout_height="wrap_content" android:inputType="number" android:ems="10" android:id="@+id/koefC" android:hint="Enter here third koef" android:layout_below="@+id/koefB" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginBottom="46dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get Result" android:id="@+id/getResult" android:layout_alignTop="@+id/koefB" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:onClick="getResultQuad"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/result" android:layout_below="@+id/koefC" android:layout_centerHorizontal="true" /> 

Main XML:

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> 

    1 answer 1

    1. You have onClick exposed in XML. Never do that. Your mistake arose because of this.
    2. In the Button getResult; field Button getResult; Nothing is recorded, but you try to use it:

       getResult.findViewById(R.id.result); getResult.setOnClickListener(this); 
    3. Your error indicates that the getResultQuad(View) method was not found. This is because the markup is set to onClick :

       android:onClick="getResultQuad"