-3

every time i tried to create a simple app , and using Button to jump to another activity its shows me "unfortunately app name has stopped"

once i delete the button code and keep everything as same as it is .

the app run perfectly , and when press the button show "unfortunately app name has stopped"

 package com.example.OWA.tesst;

 import android.content.Intent;
 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.Button;


 public class MainActivity extends ActionBarActivity {
 Button N = (Button)findViewById(R.id.B1);

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


public void direct(View v){
    Intent I = new Intent(this,sec.class);
    startActivity(I);
}

}

layout code

   <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Click"
   android:id="@+id/B1"
   android:onClick="direct"
   android:layout_centerVertical="true"
   android:layout_centerHorizontal="true" />
Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
OWA
  • 45
  • 6
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Mar 07 '15 at 22:51

1 Answers1

1

Because you have to put

Button n=(Button)findViewById(R.id.B1); 

under

setContentView(R.layout.activity_main);

I think,but remember that

Button n=(Button)findViewById(R.id.B1);

there is in the parenthesis.

guradio
  • 15,524
  • 4
  • 36
  • 57
Termi
  • 38
  • 2
  • 4