0

I am trying a very simple example. I have two activities one is apples class and one is bacon class. I am using button on apples class to open the bacon class activity.

The code is the apples class is as follows:

package jdexamples.app12_intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;

public class Apples extends AppCompatActivity {

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

    public void onClick(){
        Intent i = new Intent(this, Bacon.class);
        startActivity(i);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_apples, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

The xml for apples class looks as follows:

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Apples"
    android:background="#00FF00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/applesText"
        android:id="@+id/applesText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="29dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/applesButton"
        android:id="@+id/applesButton"
        android:layout_below="@+id/applesText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:onClick="onClick"
        android:nestedScrollingEnabled="false" />
</RelativeLayout>

Whenever I open in emulator it shows like following but when I clik on button the program crashes: Not sure what's going on here.

enter image description here

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Jd Baba
  • 5,948
  • 18
  • 62
  • 96
  • please provide the log cat... or check your manifest file – Mustanser Iqbal Oct 14 '15 at 05:27
  • Possible duplicate of [How to open a second activity on click of button in android app](http://stackoverflow.com/questions/13194081/how-to-open-a-second-activity-on-click-of-button-in-android-app) – Emil Oct 14 '15 at 05:29

4 Answers4

4

This should be like

public void onClick(View v){

You need to pass View as a Argument in onClick(...)

M D
  • 47,665
  • 9
  • 93
  • 114
1

please change below line in your layout

android:onClick="onClickButton"

do below change in your java class ,

public void onClickButton(View V){
        Intent i = new Intent(this, Bacon.class);
        startActivity(i);
    }

because onClick will override method so that is reason to change name.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
1

try this one :

public void onClickButton(View v){
    Intent i = new Intent(this, Bacon.class);
    startActivity(i);
}
Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33
Pankaj K Sharma
  • 240
  • 1
  • 12
1
package jdexamples.app12_intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;

public class Apples extends AppCompatActivity {

    Button send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apples);
        send = (Button)findViewById(R.id.applesButton);
        send.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View v) {
                Intent i = new Intent(this, Bacon.class);
                startActivity(i);
            }
        @Override
        `enter code here`public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_apples, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}