gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:08:49 25 4
gpt4 key购买 nike

我正在尝试启动我的应用程序,但我一直收到运行时错误。程序的定义是:当用户点击按钮时,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>

错误日志 enter image description here

最佳答案

错误消息指出您正在系统服务可用之前对其进行调用。这是因为您在 onCreate() 方法中调用它们。将您在 Menu.java 类中进行的调用(此处未包含,因此无法准确告诉您正在进行哪些调用)到另一个生命周期方法 - onCreateView() 可能最适合 Activity(onAttach() 是对于 Fragments 来说是一个很好的选择)。

关于java - Android 在启动应用程序时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22052738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com