2014-06-06 39 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