0

I have two activity MainActivity.java and ListActivity.java.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button)findViewById(R.id.nextButton);
    button.setOnClickListener(new AdapterView.OnClickListener(){
        public void onClick(View view){
            startAc(view);
        }
    });
}
public void startAc(View view){
    Intent intent = new Intent(this, ListActivity.class);
    startActivity(intent);
}

As soon as I click the button, the Emulator gives an error Application has stopped working.

Is the use of Intent inappropriate?

DarwinLouis
  • 1,111
  • 9
  • 15
Sujin Shrestha
  • 153
  • 1
  • 11
  • 1
    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 If I had to guess, your problem is that Android already has a class named `ListActivity`, and you are getting your two `ListActivity` classes confused. – CommonsWare Oct 21 '15 at 17:29
  • 1
    have you declare the new Activity on the ManifesT? – Itzik Samara Oct 21 '15 at 17:30
  • @ItzikSamara yes its all been declared in the Manifest. – Sujin Shrestha Oct 21 '15 at 17:40
  • Please post the Cat-log – Itzik Samara Oct 21 '15 at 18:37
  • @ItzikSamara `E/AndroidRuntime: FATAL EXCEPTION: main E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sujin.fraglistrevision/com.example.sujin.fraglistrevision.ListActivity2}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)` – Sujin Shrestha Oct 22 '15 at 07:04
  • mate your error is not here but on the other Activity when you start your Adapter please edit your post with the ListActiviytiy Code – Itzik Samara Oct 22 '15 at 16:10

2 Answers2

1

Try this code your wrong import your are import adapterview please import correct View.OnClickListener

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.nextButton);
button.setOnClickListener(new OnClickListener(){
    public void onClick(View view){

    Intent intent = new Intent(getapplicationcontext(), ListActivity.class);
    startActivity(intent);
                }
    });
   }

Add Your Manifest this line after closing first activity

   <activity android:name=".ListActivity.class"/>
Amit Basliyal
  • 840
  • 1
  • 10
  • 28
0

try changing this to MainActivity.this. Also make sure every activity is defined in manifest

haseeb
  • 682
  • 5
  • 16