-2
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getActionBar();
        actionBar.hide();
       }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

//The above is the main activity//

package com.example.scoreboard;

public class Detail {

}

//The above is my second activity//

 <application
        android:allowBackup="true"
        android:icon="@drawable/cri3"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".detail"
            android:label="@string/app_name" >

        </activity>
    </application>

//The aboveis my manifest file.//

My task is to go to second activity by clicking the button in main activity. What are the changes I have to make to complete my task. Help me I am very new to android.

2 Answers2

2

In your code you have made the following mistakes

  1. Your second activity is not extending Activity Class
  2. you have not override onCreate() method in second actvity
  3. You have not written a code to move to second activity

Refer this for an example

Moving from one activity to another Activity in Android

Community
  • 1
  • 1
Fahim
  • 12,198
  • 5
  • 39
  • 57
0

Put this code in the OnCreate of your MainActivity this code:

        Button button = (Button) findViewById(R.id.my_button);
        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });

And this should be your second activity:

       package com.example.scoreboard;

       public class Detail extends ActionBarActivity{
           @Override
           protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_detail);
           }
       }

If you want to go back to MainActivity you should use finish();

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Thomas Vos
  • 12,271
  • 5
  • 33
  • 71
  • And if you are going back to mainactivity you should use `finish();`, because otherwise you would make endless 'layers' of your app. – Thomas Vos Mar 08 '15 at 09:51