0

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! :)

2
  • Is your activity declared in your manifest.xnl? Also, you can't get your button before calling setContentView() Commented Feb 23, 2013 at 18:22
  • Can you post a stacktrace please? Commented Feb 23, 2013 at 18:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Maximii77: Quoting myself, "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".
0

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

5 Comments

Then you need to post the logcat so we know what is causing the crash
Here is an excerpt of my Manifest! <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="kops.sms.SendActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
Thanks, that helps but it is not your logcat. I have updated my answer
I can't reallly read the logcat... It always updates the error so it scrolls all the time. Is there a way to stop that? But there is always a "Device Config Tag"
You are missing part of the logcat at the beginning that is important. What is at line 18, in onCreate()? Or edit OP with updated onCreate() and maybe the xml for that activity. It looks like you have something wrong with the layout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.