Kyle Thomas Kyle Thomas - 1 month ago 14
Android Question

Unexpected Intent behavior - error onClick

I have two buttons, yes and no. When I click the yes button, it works fine and loads the next activity. However, when I press no it errors. I have switched the XML

onclick
on the no button to the yes method and it works fine so it can't me the XML, can it?

Inside my FlowIntent.java:

class FlowIntent {
public Intent Yes;
public Intent No;
}


Inside my MainActivity.java:

package capri.capritestapplication;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private FlowIntent Flow = new FlowIntent();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// should be dynamic
this.Flow.No = new Intent(this, MainActivity2.class);
this.Flow.Yes = new Intent(this, MainActivity2.class);
}
public void loadQuestionNo(View view) {
startActivity(this.Flow.No);
}
public void loadYes(View view) {
startActivity(this.Flow.Yes);
}
}


Inside my MainActivity2.java AND MainActivity3 (which works for 2):

package capri.capritestapplication;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private FlowIntent Flow = new FlowIntent();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
}
public void loadQuestionNo(View view) {
// todo
}
public void loadYes(View view) {
// todo
}
}


Inside my XML:

<Button
android:text="No"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp"
android:id="@+id/button2"
android:elevation="3dp"
android:onClick="loadYes" />

<Button
android:text="No"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp"
android:id="@+id/button"
android:elevation="3dp"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:onClick="loadQuestionNo" />


My error log is:


E/AndroidRuntime: FATAL EXCEPTION: main
Process: capri.capritestapplication, PID: 2384
java.lang.IllegalStateException: Could not find method loadQuestionNo(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
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:5610)
at android.view.View$PerformClick.run(View.java:22260)
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)

Answer
    <Button
        android:text="No"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="34dp"
        android:layout_marginStart="34dp"
        android:id="@+id/button"
        android:elevation="3dp"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:layout_toEndOf="@+id/button2"
        android:onClick="loadYes" />

<Button
        android:text="No"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="34dp"
        android:layout_marginStart="34dp"
        android:id="@+id/button"
        android:elevation="3dp"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:layout_toEndOf="@+id/button2"
        android:onClick="loadQuestionNo" />

Look at your XML code.Both button has same id android:id="@+id/button". Your Button id must be unique for every view

As your updated question i suggest you to change the name of the method which you call from XML. change your method name in xml as well as in your activities.

It would be better if you show complete code with xml that would help us to suggest you edits.

Comments