Maximii77
Maximii77

Reputation: 93

Android App Runtime Error

I have 2 classes for an Android project. The first class is the Activity and the second class is just a OnClickListener which implements the interface.

If I run the project on my phone I always get an runtime error. Also I got the message:

The specified activity does not exist! Getting the launcher activity.

Here are my two classes

SendActivity

package kops.sms;

//import android.R;
import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;

public class SendActivity extends Activity {


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

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

    buttonSend.setOnClickListener(new ButtonListener());
}



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




}

and the ButtonListener

package kops.sms;

import android.view.View;
import android.view.View.OnClickListener;

public class ButtonListener implements OnClickListener {

     @Override
        public void onClick(View v)
        {

        }


}

I don´t know what is wrong...

I look forward to your replies! :)

Upvotes: 0

Views: 1349

Answers (2)

codeMagic
codeMagic

Reputation: 44571

Be sure that your Activity is declared in your manifest. Also, change your onCreate()

public class SendActivity extends Activity {


Button buttonSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send);
    buttonSend = (Button) findViewById(R.id.buttonSend);
    buttonSend.setOnClickListener(new ButtonListener());
}  

You can't call a View such as a Button before you call setContentView() as it exists in your Layout and you haven't inflated your Layout until you call setContentViewe(). If these don't fix your problem then please post Logcat

Edit

Unless I missed it, you need to have all of your Activitys in your manifest. Something like:

<activity
        android:name="your.package.name.SendActivity"
       // activity attributes such as config changes, lable, etc...
</activity>

Logcat

Logcat output can be one of the most important pieces to determining a crash. It lists what the error was and a line number with activity where the problem occurred. If using Eclipse,

Window-->Show View-->Other-->Android-->Logcat

If you copy/paste the Logcat using coding brackets, it makes getting help much easier. You can also set filters for the logs so you don't get every single message and it is much more manageable. For example, I have a filter with: Filter Name: Runtime, by Log Tag: AndroidRuntime, by Log Level: error. This gives me only error messages for runtime errors/crashes. These filters are on the left side of the logcat view. Hope this helps

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006574

You cannot call findViewById() until after you call setContentView(). Please move:

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

to after:

setContentView(R.layout.activity_send);

and before:

buttonSend.setOnClickListener(new ButtonListener());

Also, in the future, please use LogCat (e.g., in the DDMS perspective in Eclipse) to examine the Java stack trace associated with your crashes. You would have been told about your NullPointerException, and that may have helped you to fix your problem.

Upvotes: 1

Related Questions