Jock Stiff
Jock Stiff

Reputation: 79

Android Application Crashing

I'm trying to make my application start another class.

What im trying to learn is how to get another class to run in the background - like if the user opens the application, the application stays running.

I thought if I could try to open another class by using an intent, it would work. When i run my application on the emulator, it just crashes...

Here is the opening:

package omg.justry;

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

public class MainActivity extends Activity {

@Override
    //super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    public void onCreate(Bundle savedInstanceState) {
        Intent openStartingPoint = new Intent("omg.justtry.PartF**king2");
        startActivity(openStartingPoint);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

Here is the "PartF**king2" class:

package omg.justry;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

public class PartF**king2 extends Activity{
public void onCreate(Bundle savedInstanceState) {
    Context context = getApplicationContext();
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}
    }

The thing is, Eclipse doesn't show any errors. I just exported the app and installed it to the emulator using adb.

I also added the class to the AndroidManifest as you see here:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="omg.justry"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
    android:name=".MainActivity"
    android:label="@string/title_activity_main" >
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    <activity android:name="PartF**king2"></activity>
</application>

I think its the manifest now that I look at it but whatever i do, it gets an error or crashes with Eclipse not explaining anything.

Upvotes: 0

Views: 106

Answers (1)

Orab&#238;g
Orab&#238;g

Reputation: 12012

In every class, you onCreate(Bundle savedInstanceState) method MUST contain

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

The super is absolutly mandatory, and the setContentView defines the layout for your activity.

And an Activity cannot "run in the background". Start by reading some Android tutorial, and you'll have some clues about what to do.

Upvotes: 1

Related Questions