2014-02-26 253 views
0

我試圖啓動我的應用程序,但我一直在運行時出現錯誤。 該程序的定義是:應用程序啓動時,用戶點擊按鈕,按鈕將不可見和倒計時完成後開始倒計時Menu.java將打開。Android在啓動應用程序時出現運行時錯誤

這裏是我下面的代碼:

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

+2

什麼是錯誤?發佈堆棧跟蹤 –

+0

@ Archie.bpgc我添加了錯誤日誌的圖片 –

+0

您的錯誤所在的類不包括在內(這是Menu.java) – Booger

回答

2

該錯誤消息指出您正在爲系統服務的調用,可用之前他們。這是因爲你在onCreate()方法中調用它們。將您在Menu.java類中創建的調用移動到另一個生命週期方法中 - onCreateView()可能適用於Activity(onAttach()是一個好的一個片段)。

+0

我是Android編程的新手,所以我沒有太多爭論的地方,但是......我沒有在堆棧中看到'onCreate()',它看起來像是從一個_constructor_('Menu。')... – ajb

+0

我相信這是你的問題,並且認爲你可能需要更多地瞭解Android Activity生命週期:http://developer.android.com/training/basics/activity -lifecycle/index.html的。請記住,該方法不一定存在,直到你創建它(因爲你將重寫基類中的某些東西)。底線,將呼叫轉移到您的服務以不同的方法。 – Booger

相關問題