我正在尝试启动我的应用,但我一直都会遇到运行时错误。 该程序的定义是:当用户点击按钮时,应用程序启动,按钮将不可见,并且在倒计时结束后开始倒计时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>
错误日志
答案 0 :(得分:2)
错误消息指出您在系统服务可用之前正在调用它们。这是因为您在onCreate()方法中调用它们。移动您在Menu.java类中进行的调用(不包括在这里,因此无法确切地告诉您正在调用哪些调用)到另一个生命周期方法 - onCreateView()可能最适合于一个Activity(onAttach()是一个碎片很好。