2014-02-26 10 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

무엇이 오류입니까? Stacktrace 게시 –

+0

@ Archie.bpgc 오류 로그 그림 추가 –

+0

오류가있는 클래스 (Menu.java) – Booger

답변

2

오류 메시지가 가능하기 전에, 시스템 서비스를 호출하는 것을 말한다. onCreate() 메서드에서 호출하기 때문입니다. Menu.java 클래스에서 작성한 호출을 이동하여 (여기에 포함되지 않으므로 어떤 호출을 작성하는지 정확하게 알 수 없습니다) 다른 라이프 사이클 메소드로 이동하십시오. onCreateView()는 Activity에 가장 좋습니다 (onAttach()는 파편을위한 좋은 것).

+0

나는 안드로이드 프로그래밍에 익숙하지 않기 때문에 너무 논쟁의 여지가 없다. 그러나 스택에'onCreate()'가 보이지 않는다. _constructor_ ('Menu. ') ... – ajb

+0

이것이 귀하의 문제라고 확신하며 Android Activity 라이프 사이클에 대해 좀 더 이해해야한다고 생각합니다. http://developer.android.com/training/basics/activity -lifecycle/index.html. 기본 클래스에서 무언가를 재정의 할 것이므로 메소드를 작성할 때까지 메소드가 반드시 존재하지는 않습니다. 결론은, 서비스 호출을 다른 방법으로 이동하십시오. – Booger

관련 문제