简体   繁体   English

Android应用程式当机

[英]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: 这是“ PartF ** king2”类:

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. 问题是,Eclipse没有显示任何错误。 I just exported the app and installed it to the emulator using adb. 我刚刚导出了该应用程序,并使用adb将其安装到了模拟器。

I also added the class to the AndroidManifest as you see here: 我还将类添加到AndroidManifest中,如下所示:

<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. 我认为,现在我看到它的清单了,但是无论我做什么,它都会出错或由于Eclipse无法解释而崩溃。

In every class, you onCreate(Bundle savedInstanceState) method MUST contain 在每个类中,您的onCreate(Bundle savedInstanceState)方法必须包含

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

The super is absolutly mandatory, and the setContentView defines the layout for your activity. 超级绝对是必需的,并且setContentView定义活动的布局。

And an Activity cannot "run in the background". 并且活动不能“在后台运行”。 Start by reading some Android tutorial, and you'll have some clues about what to do. 从阅读一些Android教程开始,您将获得一些提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM