-9

I am new to android development.

I am trying to create an app in which on button click I will be able to go to other page or activity. App crashes when I click compute or algorithm button.

Please let me know what I am doing wrong.

package com.example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class Main3Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
    }

    public void algorithm1(View view) {
        Intent intent = new Intent(this, algorithm.class);
        startActivity(intent);
    }

    public void compute(View view){
        Intent intent = new Intent(this,result.class);
        startActivity(intent);
    }
}

xml code for button

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Algorithm"
    android:id="@+id/button7"
    android:layout_gravity="center_horizontal"
    android:onClick="algorithm1"/>
Daniel Puiu
  • 962
  • 6
  • 21
  • 29

1 Answers1

0

You can implement the onClick using java code also. You can make use of the sample code given below:

Button btn = (Button) findViewById(R.id.button7);

btn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      yourMethod(v);
   }
});

public void yourMethod(View v) {
   // does something
}

Give this a try and let us know if it working this way. Refer the link for more information:

Community
  • 1
  • 1
Swathin
  • 516
  • 1
  • 8
  • 23