繁体   English   中英

Android启动应用程序时出现运行时错误

[英]Android Getting Runtime Error While Starting App

我正在尝试启动我的应用程序,但我一直在收到运行时错误。 程序的定义是:当用户点击按钮时,应用程序启动,按钮将不可见,并且在倒数完成后将开始倒计时Menu.java将打开。

这是我的代码如下:

Main.java

public class Main extends Activity {

    TextView countDown;
    int counter;
    Intent menuIntent;
    Button appStartButton;

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

        menuIntent = new Intent("com.example.project_21.MENU");
        counter = 5;
        countDown = (TextView) findViewById(R.id.countDown);
        appStartButton = (Button) findViewById(R.id.appStartButton);
        appStartButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                v.setVisibility(View.GONE);
                while (counter != 0) {
                    sleep(1000);
                    counter--;
                    countDown.setText(counter + " seconds");
                }
                startActivity(menuIntent);
            }
        });

    }

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

    public void sleep(long mill) {
        try {
            Thread.sleep(mill);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.project_21"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.project_21.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.project_21.Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.project_21.MENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/android2"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <TextView
        android:id="@+id/startsInfo"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:gravity="center"
        android:text="@string/welcome"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/countDown"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/countDown"
        android:textSize="45sp" />

    <Button
        android:id="@+id/appStartButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/appStartButton"
        android:textSize="35sp" />


</LinearLayout>

错误日志 在此输入图像描述

错误消息指出您在系统服务可用之前正在调用它们。 这是因为您在onCreate()方法中调用它们。 移动您在Menu.java类中进行的调用(不包括在这里,因此无法确切地告诉您正在调用哪些调用)到另一个生命周期方法 - onCreateView()可能最适合于一个Activity(onAttach()是一个片段好的一个)。

暂无
暂无

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

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