0

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.

4

1 回答 1

1

在每个类中,您的onCreate(Bundle savedInstanceState)方法必须包含

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

超级是绝对强制性的,setContentView 定义了您的活动的布局。

并且 Activity 不能“在后台运行”。从阅读一些 Android 教程开始,您将获得一些关于该做什么的线索。

于 2012-09-15T21:11:35.050 回答