Iancovici
Iancovici

Reputation: 5731

Why does android app crash after adding button for launching new activity?

real noob over here. I'm trying to create a second activity to launch the second layout. The instant I try adding the setOnClockListener, launching the app goes to a dark screen, never really loads up, then crashes.

There are two layouts: activity_main.xml and activity_camcord.xml. I posted the MainActivty, second activity CamcordActivity and the Manifest.

Please let me know whatever else I can do to make this informative enough

Main Activity

package com.notebook.ksen;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

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

        //Retrieve the button object
        Button imageButtonCamCord = (Button)findViewById(R.id.imageButtonCamCord);
        //Attach the Listener
        imageButtonCamCord.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this , CamcordActivity.class);
                startActivity(intent);
            }
        });



}

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);

    setContentView(R.layout.activity_main);


    return true;
}

}

SecondActivity

package com.notebook.ksen;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class CamcordActivity extends Activity {

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


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

}

Manifest

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.notebook.ksen.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 android:name=".CamcordActivity" android:label="camcord"/> </activity>
</application>

Upvotes: 2

Views: 1559

Answers (2)

Mongi Zaidi
Mongi Zaidi

Reputation: 1041

Try this :

ImageButton imageButtonCamCord = (ImageButton)findViewById(R.id.imageButtonCamCord);

instead of this :

Button imageButtonCamCord = (Button)findViewById(R.id.imageButtonCamCord);

If it doesn't solve the problem, try to post the Logcat output.

Upvotes: 2

okarakose
okarakose

Reputation: 3742

If you use ImageButton in main.xml, fix this;

Main Activity

ImageButton imageButtonCamCord = (ImageButton )findViewById(R.id.imageButtonCamCord);

if it does not work

Main Activity

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);

//setContentView(R.layout.activity_main); ** don't use


return true;
}

Second Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.camcord, menu);
    //setContentView(R.layout.activity_camcord); ** don't use
    return true;
}

Upvotes: 3

Related Questions