2014-06-06 40 views
0

我是新來的android開發,我不明白這個錯誤,我正在做一個簡單的秒錶應用程序,它有一個計時器和3個按鈕(開始,停止,重置)我在主要的java文件中正確定義所有這些他們的正確標籤。Android類拋出異常錯誤,當我正確投射?

它的奇怪之處在於我之前測試過應用程序,而且我的應用程序工作正常,但隨後我重新定位了GUI中的按鈕,現在它說我沒有正確投射天文臺。

06-06 19:11:07.488: E/AndroidRuntime(21511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shitstopwatch/com.example.shitstopwatch.ShittyClockActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.Chronometer 

這裏是主要的Java文件:

public class ShittyClockActivity extends ActionBarActivity { 


Button Start; 
Button Stop; 
Button Reset; 

Chronometer mainChronometer; 


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

    mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer); 

    Start = (Button) this.findViewById(R.id.startButton); 
    Stop = (Button) this.findViewById(R.id.stopButton); 
    Reset = (Button) this.findViewById(R.id.resetButton); 



    addButtonListeners(); 

} 

private void addButtonListeners() 
{ 
    Start.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 

      int stoppedMilliseconds = 0; 
      mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds); 

      mainChronometer.start(); 
     } 

    }); 

    Stop.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      mainChronometer.stop(); 

     } 

    }); 

    Reset.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      mainChronometer.setBase(SystemClock.elapsedRealtime()); 


     } 

    }); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.shitty_clock, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
}public class ShittyClockActivity extends ActionBarActivity { 


Button Start; 
Button Stop; 
Button Reset; 

Chronometer mainChronometer; 


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

    mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer); 

    Start = (Button) this.findViewById(R.id.startButton); 
    Stop = (Button) this.findViewById(R.id.stopButton); 
    Reset = (Button) this.findViewById(R.id.resetButton); 



    addButtonListeners(); 

} 

private void addButtonListeners() 
{ 
    Start.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 

      int stoppedMilliseconds = 0; 
      mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds); 

      mainChronometer.start(); 
     } 

    }); 

    Stop.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      mainChronometer.stop(); 

     } 

    }); 

    Reset.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      mainChronometer.setBase(SystemClock.elapsedRealtime()); 


     } 

    }); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.shitty_clock, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

,這裏是活動的XML與ID標籤:

<RelativeLayout 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: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="com.example.shitstopwatch.ShittyClockActivity$PlaceholderFragment" > 

<Chronometer 
    android:id="@+id/mainChronometer" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="34dp" 
    android:text="@string/chronometer" 
    android:textSize="@dimen/BigText" /> 

<Button 
    android:id="@+id/startButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/mainChronometer" 
    android:layout_below="@+id/mainChronometer" 
    android:layout_marginTop="63dp" 
    android:text="@string/startButton" /> 

<Button 
    android:id="@+id/resetButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/startButton" 
    android:layout_below="@+id/startButton" 
    android:layout_marginTop="16dp" 
    android:text="@string/resetButton" /> 

<Button 
    android:id="@+id/stopButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/resetButton" 
    android:layout_below="@+id/resetButton" 
    android:layout_marginTop="20dp" 
    android:text="@string/stopButton" /> 

</RelativeLayout> 

回答

0

清理項目(例如,項目>清除在Eclipse )。

有時,R常數與APK文件中的資源條目不同步,從而導致此錯誤。

+0

謝謝你的工作! – user14864